Fast and Slow Pointers
One pointer moves twice as fast as the other. Finds middles and cycles in one pass.
Decision · step 2 of 5Fast and Slow Pointers: Find the middle of seven
Step 1: slow moves to 2, fast jumps two to 3.
What you will see
The fast pointer laps the slow one in a cycle, or reaches the end when the slow one is halfway.
Cost
| Best | O(n) |
|---|---|
| Average | O(n) |
| Worst | O(n) |
| Space | O(1) |
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next.
Screen readers: Pointers announce their name and index; each step announces which pointer moved and why.
Reduced motion: Pointers appear at their new index with a brief emphasis instead of gliding.
Before this
Leads to
Topics that need this one first.
Taught by the same lesson
Fast and Slow Pointers covers these too, in the same run.