Fast and Slow Pointers Pattern
Fast and Slow Pointers (also called Floyd's Tortoise and Hare) advances two pointers through a sequence at different speeds, with the fast pointer moving two steps for every one step of the slow pointer. It detects cycles, finds middle elements, and locates start-of-cycle nodes in O(n) time and O(1) extra space.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Complexity
- Time
- O(n)
- Space
- O(1)
When to use Fast and Slow Pointers
- You need to detect whether a linked list, sequence, or implicit functional graph contains a cycle.
- You need to find the middle node of a singly linked list in one pass without computing its length first.
- You need to find the entry point of a cycle once one is detected.
- You need to detect a happy number, fixed point, or any repeating sequence under a deterministic transition function.
- You cannot use extra space proportional to the input (so a visited-set approach is disqualified).
Template
function hasCycle(head) {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
return true;
}
}
return false;
}Example LeetCode problems
| # | Problem | Difficulty | Frequency |
|---|---|---|---|
| 141 | Linked List Cycle | easy | foundational |
| 142 | Linked List Cycle II | medium | frequently asked |
| 876 | Middle of the Linked List | easy | foundational |
| 202 | Happy Number | easy | classic |
| 287 | Find the Duplicate Number | medium | company favorite |
| 234 | Palindrome Linked List | easy | frequently asked |
| 143 | Reorder List | medium | frequently asked |
Common mistakes
- Advancing fast by `fast.next.next` without first checking that `fast.next` is non-null, which throws a null reference on even-length lists.
- Returning the meeting point as the cycle start; the meeting point and cycle entry differ by a head-start that you must re-walk from the head.
- Using slow == fast as a starting condition (both begin at head), which would immediately return true on the first iteration.
- Choosing this pattern when the input is not a singly linked sequence and a hash set would be both simpler and equally fast.
- Forgetting that the middle-finding template returns the upper-middle on even-length lists by default — adjust the loop if you want the lower-middle.
Frequently asked questions
How does Floyd's algorithm find the start of a cycle, not just whether one exists?
Once slow and fast meet inside the cycle, reset one pointer to the head and advance both at the same speed. The node where they meet again is the cycle entry. This works because of a distance identity between the head, the cycle entry, and the meeting point.
Why use Fast and Slow Pointers when a hash set is easier to write?
A hash set takes O(n) extra space; Fast and Slow Pointers takes O(1). For very long lists or memory-constrained environments, the constant-space variant is the canonical interview answer.
Does the fast pointer always have to move two steps?
Two steps is the standard. For specialized problems you can use other speeds, but doubling guarantees that the pointers converge inside any cycle within one full revolution, which is what makes the analysis clean.
Can this pattern work on arrays interpreted as functional graphs?
Yes. Find the Duplicate Number (LC 287) treats nums as `i -> nums[i]` and runs Fast and Slow Pointers over that implicit graph to detect and locate the repeating value.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill the Fast and Slow Pointers pattern under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →