Fast & Slow Pointers

Fundamentals

Cycle 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. 1.Initialize `slow = head`, `fast = head`.
  2. 2.Advance `slow = slow.next` and `fast = fast.next.next` inside `while fast and fast.next`.
  3. 3.If `slow == fast`, a cycle is detected.
  4. 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. 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
Easy·30 companies·Max freq 100%·Acc 59.9%
BlackRockXNike+27
Easy·26 companies·Max freq 100%·Acc 54.7%
YahooNeetCode 150NeetCode 150+23
Medium·24 companies·Max freq 90%·Acc 64.6%
NianticNeetCode 150NeetCode 150+21
Easy·18 companies·Max freq 91%·Acc 58.3%
IXLAMDIntel+15
Easy·13 companies·Max freq 88%·Acc 82.1%
Walmart LabsAMDAccenture+10
Medium·13 companies·Max freq 77%·Acc 58.5%
PaytmVMwareAmerican Express+10
Medium·4 companies·Max freq 38%·Acc 37.8%
Goldman SachsAmazonBloomberg+1
Showing 7 of 7 problems in Fast & Slow PointersFiltered: All Companies