53 / 75
Easy
Linked List Cycle
#53linked-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.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
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