Fast & Slow Pointers
FundamentalsCycle detection and midpoint traversal using Floyd's Tortoise and Hare algorithm
7 problems·4 Easy·3 Medium·0 Hard
Pattern Study Guide & Cheat Sheet▼
Advance two pointers at different speeds (usually 1 step and 2 steps) to detect cycles or locate the middle node in O(N) time and O(1) space.
Core Invariant: In a cycle of length C, each iteration closes the gap between fast and slow by 1. Fast catches slow in <= C steps. Distance from head to cycle entry equals distance from collision point to cycle entry.
Recognize it (Keywords & Signals)
- Detect cycle in a Linked List
- Find the exact start / entry node of a cycle
- Find middle node of a Linked List in a single pass
- Find duplicate number in array without modifying it (Floyd cycle)
- Happy Number (detect repeating loop in sum of squared digits)
When NOT to use
Need random access or already have O(N) auxiliary space permitted to use a Hash Set.
How to solve (Step-by-step)
- 1.Initialize `slow = head`, `fast = head`.
- 2.Advance `slow = slow.next` and `fast = fast.next.next` inside `while fast and fast.next`.
- 3.If `slow == fast`, a cycle is detected.
- 4.To find cycle start: reset `slow = head`. Move both `slow` and `fast` 1 step at a time until they meet. The meeting node is the cycle entry.
- 5.For middle node: when fast reaches the end, slow is at the middle.
Watch for (Interview Traps)
- Dereferencing `fast.next.next` without checking both `fast` and `fast.next` (causes AttributeError)
- Even-length lists: slow stops at the second middle node if starting both at head
- Assuming an array has no cycle without checking bounds when modeling indices as pointers
Floyd's Cycle Detection & Entry Finder
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def detect_cycle_entry(head: ListNode | None) -> ListNode | None:
slow = fast = head
# Phase 1: Determine if a cycle exists
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break
else:
return None # Reached end of list -> No cycle
# Phase 2: Find cycle entry node
slow = head
while slow != fast:
slow = slow.next
fast = fast.next
return slow # Both pointers meet at cycle entrance- Cost
- O(n) linear scan · O(1) strictly constant space (Guarantees O(1) space where a visited hash set would take O(n).)
Canonical problems
#141 Linked List Cycle: Verify if fast pointer collides with slow pointer
#142 Linked List Cycle II: Reset slow to head to find exact entry node
#876 Middle of the Linked List: When fast hits the end, slow is at the midpoint
#287 Find the Duplicate Number: Treat array indices as pointers and find cycle entrance
⌘K
Medium·24 companies·Max freq 90%·Acc 64.6%
NianticNeetCode 150NeetCode 150+21
# | Problem | Difficulty | Top Companies↓ | Frequency | Acceptance | |
|---|---|---|---|---|---|---|
| #202 | Happy Number Hash TableMathTwo PointersFloyd's Cycle Finding Algorithm | Easy | 100% | 59.9% | ||
| #141 | Linked List Cycle Hash TableLinked ListTwo PointersFloyd's Cycle Finding Algorithm | Easy | 100% | 54.7% | ||
| #287 | Find the Duplicate Number ArrayTwo PointersBinary SearchBit ManipulationPigeonhole PrincipleFloyd's Cycle Finding Algorithm | Medium | 90% | 64.6% | ||
| #234 | Palindrome Linked List Linked ListTwo PointersStackRecursion | Easy | 91% | 58.3% | ||
| #876 | Middle of the Linked List Linked ListTwo Pointers | Easy | 88% | 82.1% | ||
| #142 | Linked List Cycle II Hash TableLinked ListTwo PointersFloyd's Cycle Finding Algorithm | Medium | 77% | 58.5% | ||
| #457 | Circular Array Loop ArrayHash TableTwo PointersFloyd's Cycle Finding Algorithm | Medium | 38% | 37.8% |
Showing 7 of 7 problems in Fast & Slow PointersFiltered: All Companies