You are viewing a single comment's thread. Return to all comments →
You can save the initial check by using a do-while
do-while
boolean hasCycle(Node head) { Node tortoise = head; Node hare = head; do{ if(hare == null || hare.next == null) return false; hare = hare.next.next; tortoise = tortoise.next; } while(hare != tortoise); return true; }
Seems like cookies are disabled on this browser, please enable them to open this website
Linked Lists: Detect a Cycle
You are viewing a single comment's thread. Return to all comments →
You can save the initial check by using a
do-while