Fast & Slow Pointers

Floyd's tortoise and hare — detect cycles, find the cycle start, the middle node, and duplicates in O(1) space.

Beginner 2 min readLesson 3 of 25

Two pointers traverse the same sequence at different speeds — typically slow moves 1 step and fast moves 2. If there's a cycle, fast laps slow and they meet. If there's no cycle, fast reaches the end. When fast reaches the end of a list, slow is in the middle.

head12slow34fast56
A list whose tail links back to node 3. The fast pointer will eventually catch the slow one inside the loop.
When to use this pattern
  • Detect a cycle in a linked list or in any "next" function (n → f(n)).
  • Find the middle of a linked list in one pass.
  • Find the start of a cycle, or the cycle length.
  • Find a duplicate number in an array treated as a function graph.
  • You're required to use O(1) extra space (otherwise a hash set also works).

Cycle detection and cycle start

boolean hasCycle(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
        if (slow == fast) return true;
    }
    return false;
}

ListNode detectCycleStart(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
        if (slow == fast) {                 // meeting point inside the cycle
            ListNode p = head;
            while (p != slow) { p = p.next; slow = slow.next; }
            return p;                       // both arrive at the cycle start
        }
    }
    return null;
}

Why resetting one pointer finds the cycle start

Let the distance from head to the cycle start be a, the meeting point be b steps into the cycle, and the cycle length c. Slow walked a + b; fast walked twice that, i.e. a + b + kc for some k. So a + b = kc, meaning a = kc − b: walking a steps from the meeting point lands exactly on the cycle start — and so does walking a steps from the head.

Middle of a linked list

ListNode middle(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;        // second middle for even lengths
}

Middle-finding powers: palindrome linked list (find middle, reverse the second half, compare), reorder list (middle, reverse, interleave), and merge sort on lists.

Beyond linked lists: cycles in functions

Any function f on a finite set eventually cycles. Happy number: repeatedly replace n with the sum of squares of its digits — fast/slow detects whether you loop without reaching 1.

Find the duplicate number (array of n + 1 values in 1..n, read-only, O(1) space): treat i → nums[i] as a linked list. The duplicate value is the entry point of the cycle.

Find the duplicate number
int findDuplicate(int[] nums) {
    int slow = nums[0], fast = nums[0];
    do { slow = nums[slow]; fast = nums[nums[fast]]; } while (slow != fast);
    slow = nums[0];
    while (slow != fast) { slow = nums[slow]; fast = nums[fast]; }
    return slow;
}
Practice problems
  • Linked List Cycleeasy
  • Middle of the Linked Listeasy
  • Happy Numbercycle in digit-square functioneasy
  • Palindrome Linked Listmiddle + reverse second halfeasy
  • Linked List Cycle IIcycle startmedium
  • Reorder Listmiddle, reverse, mergemedium
  • Remove Nth Node From End of Listgap of nmedium
  • Find the Duplicate Numberindex → value graphmedium
  • Circular Array Loopmedium
Key takeaways
  • Slow moves 1, fast moves 2: they meet iff there's a cycle.
  • To find the cycle start, reset one pointer to the head and move both by 1.
  • When fast hits the end, slow is at the middle.
  • Works on any "next" function, not just linked lists — all in O(1) space.