Fast & Slow Pointers
A pattern that uses two pointers moving at different speeds to solve complex linked list problems like cycle detection and finding the middle node.
Fast and slow pointers read one linked list at two different speeds
This pattern places two pointers on the same linked list. The slow pointer moves one node at a time, while the fast pointer moves two. That small speed difference is enough to solve two common problems: finding the middle node and detecting whether a cycle exists.
Slow advances one hop per step. Fastadvances two hops per step.
In Find Middle, the key moment is when fast reaches the end of the list. In Detect Cycle, the key moment is when the two pointers land on the same node.
In Find Middle, step until fast runs out of list. Notice where slow stops.
In Detect Cycle, watch the tail loop back into the list, then keep stepping until fast catches slow inside the cycle.
Use Auto Play to watch the motion continuously, or Reset to replay the same example.
Slow moves one node at a time while fast moves two to find the middle or detect a loop.
One speed difference solves two different linked-list questions
The setup is the same in both demos, but the goal changes. One version finds the midpoint of a finite list. The other checks whether the list ever loops back into itself.
Slow moves once while fast moves twice.
When fast reaches the end, slow has only covered half the distance.
That leaves slow at the middle node.
Slow and fast both enter the loop if a cycle exists.
Fast gains one node on slow during each step inside the cycle.
That guarantees a meeting point, which proves the cycle exists.
| Aspect | Find Middle | Detect Cycle |
|---|---|---|
| Main job | Find the middle node without first counting the whole list. | Detect whether the next pointers eventually loop back into the list. |
| Slow pointer | Moves one hop each step and ends at the middle when fast reaches the end. | Moves one hop each step and helps reveal when the two pointers meet. |
| Fast pointer | Moves two hops each step, so it reaches the end sooner. | Moves two hops each step, so it laps slow inside the cycle. |
| Why it works | Fast covers the list twice as quickly, so slow is halfway when fast finishes. | If a loop exists, two runners moving at different speeds must eventually land on the same node. |
The short version
- Fast and slow pointers use two speeds on the same linked list.
- The middle-node version works because fast reaches the end while slow only travels half as far.
- The cycle-detection version works because the faster pointer eventually catches the slower one inside the loop.
- Both patterns run in O(n) time and use O(1) extra space.