You are viewing a single comment's thread. Return to all comments →
Java solution using fast and slow pointers, no need for a HashSet.
boolean hasCycle(Node head) { if (head == null) return false; Node slow = head; Node fast = head.next; while (slow != fast) { if (fast == null || 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 →
Java solution using fast and slow pointers, no need for a HashSet.