Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
Chaturmind
← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
HomeLearnDSADSA Patterns for InterviewsCoding Patterns
✓ FreeIntermediate· 12 min read

Fast and Slow Pointers

Use Floyd's cycle detection algorithm to find cycles, middle nodes, and kth-from-end in linked lists.

Published March 22, 2025


Fast and Slow Pointers

The fast & slow pointer pattern (Floyd's Tortoise and Hare) uses two pointers moving at different speeds to detect cycles, find midpoints, and locate specific positions in linked lists.

Detect Cycle in Linked List

public boolean hasCycle(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;       // move 1 step
        fast = fast.next.next;  // move 2 steps
        if (slow == fast) return true; // cycle detected
    }
    return false; // fast reached end → no cycle
}

Find Cycle Start

Once a cycle is detected, move one pointer to head. Both then advance at speed 1 — they meet at the cycle start.

public ListNode detectCycle(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
        if (slow == fast) {
            // Cycle detected — find entry point
            ListNode entry = head;
            while (entry != slow) {
                entry = entry.next;
                slow = slow.next;
            }
            return entry;
        }
    }
    return null;
}

Middle of Linked List

public ListNode middleNode(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow; // slow is at the middle
    // For even-length list: returns second middle node
}

Palindrome Linked List

public boolean isPalindrome(ListNode head) {
    // Step 1: find middle
    ListNode slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }

    // Step 2: reverse second half
    ListNode prev = null, curr = slow;
    while (curr != null) {
        ListNode next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }

    // Step 3: compare
    ListNode left = head, right = prev;
    while (right != null) {
        if (left.val != right.val) return false;
        left = left.next;
        right = right.next;
    }
    return true;
}

Remove Nth Node from End

public ListNode removeNthFromEnd(ListNode head, int n) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode fast = dummy, slow = dummy;

    // Advance fast by n+1 steps
    for (int i = 0; i <= n; i++) fast = fast.next;

    // Move both until fast reaches end
    while (fast != null) {
        slow = slow.next;
        fast = fast.next;
    }

    // slow is just before the node to remove
    slow.next = slow.next.next;
    return dummy.next;
}

Happy Number

// A number is happy if summing squares of digits eventually reaches 1
// Unhappy numbers cycle — detect with fast/slow!
public boolean isHappy(int n) {
    int slow = n, fast = n;
    do {
        slow = digitSquareSum(slow);
        fast = digitSquareSum(digitSquareSum(fast));
    } while (slow != fast);
    return slow == 1;
}
int digitSquareSum(int n) {
    int sum = 0;
    while (n > 0) { int d = n % 10; sum += d*d; n /= 10; }
    return sum;
}

Interview Tips

  1. Fast & slow pointers use O(1) space — the key advantage over storing nodes in a HashSet.
  2. The mathematical proof for cycle detection: when fast catches slow, slow has traveled ≤ cycle length, so meeting point leads to cycle start in exactly head_to_cycle steps.
  3. The two-step approach (find middle, then reverse) is the optimal palindrome check for linked lists.

Next

Merge Intervals

AI Tutor

Lesson: Fast and Slow Pointers

Quick actions

AI responses can be inaccurate. Verify critical information.