55 / 75
Medium
Linked List Cycle II
#55linked-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=1Output:
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.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Use Floyd's algorithm: detect cycle, then find entry point by resetting one pointer to head.
Example:
Input:
head=[3,2,0,-4], pos=1Output:
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