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.

Pointer Pattern

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.

How to read the demo
Watch the two highlighted pointers move through the same list.

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.

What to try in the demo
Step through both scenarios and compare what the speed difference reveals.

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.

Slow = +1 hopFast = +2 hopsO(n) time / O(1) space
[ value | next ]Linear list
SlowFast
node
1
value
next
node
2
value
next
node
3
value
next
node
4
value
next
node
5
value
next
node
6
value
next
node
7
value
Slow moves one hop. Fast moves two hops. Step through the list to watch the gap change.
steps 0
Pattern Uses

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.

Find Middle
Useful when you need the midpoint without a separate counting pass.

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.

Detect Cycle
Useful when you need to know whether the list ever loops back.

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.

AspectFind MiddleDetect Cycle
Main jobFind the middle node without first counting the whole list.Detect whether the next pointers eventually loop back into the list.
Slow pointerMoves 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 pointerMoves two hops each step, so it reaches the end sooner.Moves two hops each step, so it laps slow inside the cycle.
Why it worksFast 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.
Key Takeaways

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.