Fast and Slow Pointers: Midpoint and Cycles
Compare one-step and two-step advances. First use them to find a midpoint, then check for a cycle.
Core Intuition & How It Works
Floyd’s tortoise-and-hare technique advances a slow pointer by one node and a fast pointer by two. In an acyclic list, slow reaches the midpoint when fast hits null; in a cyclic list, the two pointers are guaranteed to meet inside the cycle.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(n) |
| Space Complexity | O(1) |
| Topic Chapter | Chapter 4: Linked Lists |
| Difficulty Level | Beginner |
Key Pattern to Recognize
Check fast !== null && fast.next !== null before advancing fast by two steps; reset one pointer to head after a meeting to find the cycle entry.
Common Pitfalls & Edge Cases
Checking only fast !== null and crashing on fast.next.next, or off-by-one midpoint choice on even-length lists.
Real-World & Technical Interview Applications
Cycle detection in linked lists, finding list midpoints for merge sort, and detecting duplicate numbers in O(1) space.