Fast and Slow Pointers

Topics Covered

The Tortoise and Hare Concept

Why Two Speeds Work

The Mathematical Guarantee

Space Efficiency

The General Pattern

Cycle Detection

The Algorithm

Time Complexity Analysis

Space Complexity

Edge Cases

Visualizing the Chase

Finding the Middle

The Algorithm

Odd vs Even Length Lists

Why This Works: The 2x Speed Argument

Finding the Middle as a Subroutine

Cycle Start and Applications

Finding the Cycle Start

Why This Works: The Math

Application: Happy Number

Application: Palindrome Linked List

When to Recognize the Pattern

Many linked list problems seem to require extra memory. Detecting a cycle? Store every visited node in a hash set. Finding the middle? Count all nodes, then traverse halfway. These approaches work, but they use O(n) space. The fast and slow pointer technique solves both problems - and several others - with just two pointers and O(1) space.

The idea is simple: use two pointers that move at different speeds. The slow pointer advances one node per step. The fast pointer advances two nodes per step. Depending on the problem, the relationship between these two pointers reveals structural information about the list - whether it contains a cycle, where the middle is, or where a cycle begins.

Fast and slow pointers cycle detection
123456slowfastMeet!
Slow moves one step and fast moves two steps per iteration. If a cycle exists, they are guaranteed to collide inside it.

Why Two Speeds Work

Consider what happens when both pointers traverse a list with no cycle. The fast pointer moves twice as fast, so it reaches the end in half the time. When fast hits null, slow is at the midpoint. That alone solves the "find the middle" problem.

Now consider a list with a cycle. The fast pointer enters the cycle first. The slow pointer enters later. Once both are inside the cycle, think of the gap between them. Each step, slow moves forward by 1 and fast moves forward by 2. The gap between them changes by exactly 1 each step. If the cycle has length C, the gap closes from some initial value down to 0 within at most C steps. They are guaranteed to meet.

This is the key insight: in a cycle, the fast pointer does not skip over the slow pointer. Because the gap shrinks by exactly 1 per step, the fast pointer catches the slow pointer from behind on every lap. There is no scenario where fast jumps past slow.

The Mathematical Guarantee

Suppose the cycle has length C and the slow pointer enters the cycle when the fast pointer is already some distance ahead inside the cycle. Let the initial gap between fast and slow (measured in the direction of travel) be G, where G < C. Each step reduces this gap by 1. After exactly G steps, the gap is 0 - they collide. Since G < C and C <= n (total nodes), the collision happens in O(n) time.

Interview Tip

The name comes from Aesop's fable of the tortoise and the hare. The algorithm is formally called Floyd's cycle detection algorithm, invented by Robert W. Floyd in 1967. Despite the fable, the tortoise always wins here - the slow pointer is what gives us the answer.

Space Efficiency

The alternative approach to cycle detection uses a hash set: visit each node and store it, then check if the current node was already visited. This works but requires O(n) space for the hash set. The fast and slow pointer technique uses exactly two pointer variables - O(1) space regardless of list size. For a list with one million nodes, that is the difference between allocating a million-entry hash set and allocating two variables.

This space advantage is why fast and slow pointers appear so frequently in interviews. The interviewer wants to see if you can solve problems without reaching for extra data structures.

The General Pattern

Every fast and slow pointer problem follows the same template:

The loop guard while fast and fast.next is critical. It handles two cases: fast is null (even-length list, fast went past the end) and fast.next is null (odd-length list, fast is on the last node). Both conditions mean the list has no cycle, or the traversal is complete.