Find Cycle Entry
After they meet, restart one at the head and walk both by one. They meet again at the entry.
Decision · step 2 of 10Fast and Slow Pointers: Where the loop begins
Step 1: slow to 2, fast to 3.
What you will see
From the meeting point and the head, two pointers walk in step and collide exactly at the entry.
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, try operations in any order.
Screen readers: Each node announces its value and what it points to; each step announces the pointer change ("node 3 now points to node 5").
Reduced motion: Pointer arrows redraw in place with a brief emphasis instead of animating the detach and reattach.
Before this
Related
Taught by the same lesson
Fast and Slow Pointers covers these too, in the same run.