Fast slow pointer

2 snippets in Interview Prep

Also written as slow fast pointer

DSAInterview Prep

Find Middle of Linked List

DSA · Linked Lists
Syntax
Slow pointer moves 1 step, fast moves 2 steps.
When fast reaches end, slow is at the middle.
Example
// JavaScript
function findMiddle(head) {
  let slow = head, fast = head;
  while (fast && fast.next) {
    slow = slow.next;
    fast = fast.next.next;
  }
  return slow;
}

# Python
def find_middle(head):
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow
Output
1→2→3→4→5 → middle is 3
1→2→3→4 → middle is 3 (second of two middles)

Note Time O(n), Space O(1). For even-length lists, this returns the second middle node. To get the first middle, use: while fast.next and fast.next.next. This technique is a building block for merge sort on linked lists and palindrome checking.

Pattern: Fast & Slow Pointer

DSA · Common Patterns Summary
Syntax
When to use:
- Cycle detection (linked list, array)
- Finding middle of linked list
- Finding the start of a cycle
- Happy number problem

Slow moves 1 step, fast moves 2 steps.
Example
// JavaScript - Happy number (sum of squared digits eventually reaches 1)
function isHappy(n) {
  function digitSquareSum(num) {
    let sum = 0;
    while (num > 0) {
      const digit = num % 10;
      sum += digit * digit;
      num = Math.floor(num / 10);
    }
    return sum;
  }
  let slow = n, fast = n;
  do {
    slow = digitSquareSum(slow);
    fast = digitSquareSum(digitSquareSum(fast));
  } while (slow !== fast);
  return slow === 1;
}

# Python
def is_happy(n):
    def digit_square_sum(num):
        total = 0
        while num > 0:
            digit = num % 10
            total += digit * digit
            num //= 10
        return total
    slow = fast = n
    while True:
        slow = digit_square_sum(slow)
        fast = digit_square_sum(digit_square_sum(fast))
        if slow == fast:
            break
    return slow == 1
Output
is_happy(19) → True (19→82→68→100→1)
is_happy(2) → False (enters cycle)

Note The fast/slow pointer pattern detects cycles in any sequence where each value maps to the next. It uses O(1) space compared to a hash set. For linked lists, this is Floyd's algorithm. For number sequences (like happy number), the function that generates the next value plays the role of 'next pointer'.

Frequently asked questions

How does Interview Prep handle fast slow pointer?
Interview Prep covers this with 2 copy-ready snippets on this page. The "Find Middle of Linked List" snippet in Interview Prep uses `Slow pointer moves 1 step, fast moves 2 steps.`.
Which code does the Interview Prep example use?
The "Find Middle of Linked List" snippet uses `Slow pointer moves 1 step, fast moves 2 steps.`, from the Linked Lists section of the Interview Prep cheat sheet.
What other Interview Prep snippets are shown for "fast slow pointer"?
Besides "Find Middle of Linked List", this page also shows "Pattern: Fast & Slow Pointer".
Is there anything to watch out for?
Yes. For "Find Middle of Linked List": Time O(n), Space O(1). For even-length lists, this returns the second middle node. To get the first middle, use: while fast.next and fast.next.next. This technique is a building block for merge sort on linked lists and palindrome checking.