Week 1 · Day 3 of 42

Two Pointer: Fast & Slow

Linked list + "cycle" or "middle" → use slow (1 step) and fast (2 steps). They meet inside a cycle. Reset one to head and walk both at 1 step to find the cycle start.

Sign in to tick off tasks and keep your progress on every device

Two Pointers - Fast & Slow (Cycle Detection)

Tick the box when you have studied this pattern

How to recall this pattern

Linked list + "cycle" or "middle" → use slow (1 step) and fast (2 steps). They meet inside a cycle. Reset one to head and walk both at 1 step to find the cycle start.
123456slow+1 stepfast+2 stepscycle
  • Use two pointers moving at different speeds (slow = 1 step, fast = 2 steps).
  • If there is a cycle, they will eventually meet inside the cycle.
  • To find the cycle start: reset one pointer to head, then both move 1 step until they meet.
  • Also used to find the middle of a linked list (when fast reaches the end, slow is at the middle).
Python template and worked solutions

Solve these

Try each one before reading a solution. Name the pattern out loud first.

Spotted a mistake on this page?

Wrong formula, broken animation or confusing wording. Tell us and we will fix it.