You are viewing a single comment's thread. Return to all comments →
Pretty neat solution, however you may define a unique condition of the if within the while due to the fact you have already validate if head was null;
boolean hasCycle(Node head) { if (head == null) return false; Node slow = head; Node fast = head.next; while (slow != fast) { if (fast.next == null) return false; slow = slow.next; fast = fast.next.next; } 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 →
Pretty neat solution, however you may define a unique condition of the if within the while due to the fact you have already validate if head was null;