Interview patterns

Fast and Slow Pointers Pattern Explained (Tortoise and Hare)

Updated Nov 28, 2026 · 3 min read

Start two pointers at the head. Slow moves one node per step and fast moves two. If there's no loop, fast falls off the end; if there is one, fast gains a step on slow every move and must land on it. When fast reaches the end of a list, slow is in the middle. Both run in O(n) time and O(1) extra space.

Fast and Slow Pointers reel cover

Key points

  • ●Slow moves 1 node, fast moves 2.
  • ●No loop: fast falls off the end. A loop: fast catches slow.
  • ●When fast reaches the end, slow is on the middle node.
  • ●O(n) time and O(1) space, versus O(n) space for a hash set.
  • ●Practice on LeetCode 141, 142, 876, 202 and 287.
Step-by-step visual sheet: Fast and Slow Pointers
The full visual sheet, made with Edithly.Open the interactive sheet

The pattern in one line

Two pointers walk the same linked list at different speeds. Slow takes one step at a time, fast takes two. That speed gap answers two questions with zero extra memory: does this list loop, and where is its middle?

It's also called the tortoise and hare, for obvious reasons.

When to spot it

Reach for it when the problem says:

  • "does the linked list have a cycle?" or "where does the cycle start?"
  • "find the middle of the linked list" in one pass
  • a number sequence that might loop forever, like Happy Number
  • a duplicate in an array of 1 to n, like Find the Duplicate Number
  • O(1) extra space, so no hash set

How it works

There are only two cases.

No loop. The list ends somewhere. Fast moves twice as quickly, so it reaches the end first and the loop condition stops. Return False.

A loop. Fast never finds an end. Once both pointers are inside the loop, fast gains exactly one node on slow every move. The gap shrinks by one each step until it hits zero, and fast lands on slow. Return True.

The visual sheet above shows this as a flowchart: check that fast and fast.next exist, move both, test whether they're the same node, repeat.

The template in Python

def has_cycle(head):
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow is fast:
            return True   # cycle
    return False

Two details matter. Check fast and fast.next before moving, or fast.next.next crashes on a short list. And compare with is, since you're asking whether they're the same node, not whether two nodes hold equal values.

Worked example: find the middle

Take the list 1 -> 2 -> 3 -> 4 -> 5 with no loop.

Move slow fast
start 1 1
1 2 3
2 3 5

Fast is on the last node, so fast.next is empty and the loop stops. Slow is on 3, the middle. For the middle problem you return slow instead of testing for a meeting.

Odd and even lengths. With 7 nodes, slow visits 1, 2, 3, 4 while fast visits 1, 3, 5, 7. Fast stops on node 7 and slow is on 4, the exact middle.

With an even count there are two middles. On 1 to 6, fast goes 1, 3, 5 and then jumps past the end, so slow takes one more step and stops on 4, the second of the two middles. That's the node LeetCode 876 asks for. If you ever need the first middle instead, loop while fast.next and fast.next.next.

Complexity.

Fast and slow pointers Visited-node hash set
Time O(n) O(n)
Extra space O(1) O(n)

Same speed, a fraction of the memory.

Beyond linked lists

The "list" doesn't have to be made of nodes. In Happy Number, each number points to the sum of the squares of its digits, so the sequence either reaches 1 or loops forever. Run slow one step and fast two steps along that sequence and you detect the loop the same way. Find the Duplicate Number treats each array value as a pointer to the next index, which turns a repeated value into a cycle.

The trap: the meeting point isn't the cycle start

When the pointers meet, you know a loop exists. You don't know where it begins. That's LeetCode 142, a separate step after the meeting. Don't return the meeting node as the cycle start.

Practice set: 141, 142, 876, 202 and 287.

Frequently asked questions

Why does the fast pointer always meet the slow pointer in a cycle?

Once both are inside the loop, fast closes the gap by exactly one node every move, because it moves two and slow moves one. A gap that shrinks by one each step has to reach zero, so they land on the same node.

Is the meeting point the start of the cycle?

Not in general. The template only proves a loop exists. Finding where the cycle starts, LeetCode 142, is a separate step after the meeting.

Why use fast and slow pointers instead of a hash set?

A set of visited nodes also detects a cycle, but it stores up to n nodes. Two pointers use O(1) extra space with the same O(n) time, and some problems ask for constant space.

Which LeetCode problems use fast and slow pointers?

141 Linked List Cycle, 142 Linked List Cycle II, 876 Middle of the Linked List, 202 Happy Number and 287 Find the Duplicate Number.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free