Last Updated: January 18, 2026
Fast and Slow Pointers is a powerful pattern where you use two pointers and move them at different speeds.
It can help you solve multiple linked lists and array related problems like:
The beauty of this pattern lies in how efficiently it solves these type of problems.
It only uses O(n) time and O(1) space complexity, which means it's both fast and memory-efficient.
Plus, you can solve these types of problems in just one pass through the data, without needing to go over it multiple times.
In this chapter, you'll learn:
Alright, so what exactly is the Fast & Slow Pointers pattern?
To explain it, letβs use a real-world example:
Imagine two runners, A and B, on a circular track.
Runner A is running faster than Runner B.
Since the track is circular, at some point, Runner A will catch up to and pass Runner B.
This is essentially how the Fast & Slow Pointers pattern works.
It uses two pointers that traverse a sequence at different speeds. Typically:
By the time the fast pointer reaches the end (or catches up to the slow pointer in a cycle), the slow pointer will be at a position of interest, like the middle of the list or the cycle entry point.
The mathematical intuition behind fast and slow pointers is elegant:
If the fast pointer moves at 2x speed and the slow pointer at 1x speed, when the fast pointer has covered the entire distance, the slow pointer has covered exactly half. This places the slow pointer at the middle.
If there is a cycle, the fast pointer will eventually "lap" the slow pointer. The key insight from Floyd's algorithm is that if they meet, there is definitely a cycle. And the mathematics of their meeting point reveals exactly where the cycle begins.
In a cycle, both pointers will eventually enter the cycle. Since the fast pointer moves twice as fast, it will catch up to the slow pointer within the cycle. The meeting is guaranteed because the relative speed difference is 1 step per iteration.
This pattern is the right tool when you see these situations:
1. Finding the middle of a linked list
When you need to find the middle element without knowing the length in advance, fast and slow pointers give you a single-pass O(n) solution.
2. Detecting cycles in a linked list or sequence
If a sequence might contain a cycle (a node pointing back to an earlier node), fast and slow pointers can detect it in O(1) space.
3. Finding the start of a cycle
Not only can you detect a cycle, but with a clever technique you can also find exactly where the cycle begins.
4. Checking for palindromes in linked lists
Combined with list reversal, this pattern helps check if a linked list is a palindrome.
5. Finding cycles in number sequences
Some numerical problems (like Happy Number) involve sequences that might cycle. The same technique applies.
Here are common problem types where fast and slow pointers excel:
Most fast and slow pointer problems follow one of two templates. Let us establish them clearly.
Note on even-length lists: When the list has an even number of nodes, this template returns the second middle node. For example, inΒ [1, 2, 3, 4], it returns node 3. If you need the first middle node, checkΒ fast.next != null && fast.next.next != nullΒ instead.