53 / 75
E

Linked List Cycle

#53
linked-listtwo-pointershash-table

Use Floyd's cycle detection: slow and fast pointers, if they meet there's a cycle.

Example:

Input:head=[3,2,0,-4], pos=1 (tail connects to index 1)
Output:true (cycle exists)

Common Mistakes:

  • Using HashSet when two-pointer is more space-efficient
  • Not checking fast.next before fast.next.next (NPE)
  • Comparing node values instead of references
  • Starting slow and fast at different positions

Notes:

Floyd's Tortoise and Hare algorithm. Time O(n), Space O(1). Alternative: HashSet is O(n) space but simpler.

53/75
Linked List Cycle