55 / 75
M

Linked List Cycle II

#55
linked-listtwo-pointershash-table

Use Floyd's algorithm: detect cycle, then find entry point by resetting one pointer to head.

Example:

Input:head=[3,2,0,-4], pos=1
Output:Node with value 2 (cycle entry)

Common Mistakes:

  • Not understanding the math behind why resetting works
  • Moving pointers at different speeds after detection
  • Using HashSet when O(1) space solution exists
  • Forgetting to check for null before accessing next

Notes:

Math: distance from head to entry = distance from meeting point to entry. Time O(n), Space O(1). Can also use HashSet for O(n) space.

55/75
Linked List Cycle II