AlgoScope

Detect Cycle

AlgorithmintermediateLinked List Operationsalso called Floyd's tortoise and hare

If fast ever lands on slow, the list loops. On a straight list fast just runs out.

Decision · step 2 of 6Fast and Slow Pointers: A list that loops
123456headslowfast

Step 1: slow to 2, fast to 3.

Open in the player →or start at step 2

What you will see

The list loops back on itself; the fast pointer laps the slow one and they collide.

How fast and slow pointers works →

Cost

BestO(n)
AverageO(n)
WorstO(n)
SpaceO(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.

Leads to

Topics that need this one first.

Taught by the same lesson

Fast and Slow Pointers covers these too, in the same run.