This problem follows the Fast/Slow Pointers pattern, commonly found in the Linked List category. Recognizing this pattern is key to solving it efficiently in an interview setting.
Slow moves 1 step, fast moves 2 steps. If they meet, there is a cycle.
If there's a cycle, fast will 'lap' slow — the gap decreases by 1 each step, so they must meet within one loop of the cycle.
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return true
return falsePractice Linked List Cycle and similar Linked List problems with flashcards. Build pattern recognition through active recall.
Practice this problem