Slow and Fast Pointer (Floyd's Algorithm) Explained
Fast recall
Quick revision
Core answer
Familiarity with pointer-based linked-list algorithms beyond traversal, and why the two pointers must meet if a cycle exists
Only mentioning cycle detection and forgetting find-middle, or not explaining why the pointers must meet inside a cycle
Explain the relative-speed argument and name all three uses: cycle detection, finding the cycle start, and finding the middle
Interview-ready guide
Interview answer
The slow and fast pointer technique uses two pointers starting at the same position in a linked list.
Slow moves one node per step, and fast moves two nodes per step. If there's a cycle in the list, they'll eventually meet inside it. If there's no cycle, fast will reach null first.
This is called Floyd's cycle-detection algorithm or the tortoise and hare algorithm. Here's why it works. Once both pointers are inside the cycle, the gap between them shrinks by one node per step.
Fast gains two nodes each step, slow gains one, so their relative speed is one node per step. Since the cycle has finite length, the gap must reach zero eventually and they meet. The key is to check both fast not equals null and fast dot next not equals null in your while condition, because you're doing fast dot next dot next and need both hops to be safe.
The technique has three main applications. First is cycle detection, if they meet there's a cycle. Second is finding the middle node, when fast reaches the end, slow is at the middle because it traveled half the distance.
This is useful for splitting linked lists in merge sort. Third is finding where the cycle starts, after detecting a cycle you reset slow to the head and move both pointers one step at a time, and they meet at the cycle's entry point. The same movement rule also finds a middle node, and after a cycle is detected it can locate the cycle entry.
next` so an acyclic list ends safely.
For example, in 1 -> 2 -> 3 -> 4 -> 2, slow and fast move to (2, 3), then (3, 2), then (4, 4). Their meeting at node 4 proves that the list contains a cycle.
Slow and Fast Pointer (Floyd's Algorithm) Explained
Direct Answer
The slow pointer moves one step at a time while the fast pointer moves two. If they ever meet inside a linked list, a cycle exists — this is Floyd's cycle-detection. The same idea finds the middle node (slow is at the middle when fast reaches the end).
Code Example
public class LinkedListTwoPointer {
static class ListNode {
int val; ListNode next;
ListNode(int val) { this.val = val; }
}
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; // 1 step
fast = fast.next.next; // 2 steps
if (slow == fast) return true; // met inside the cycle
}
return false; // fast reached end, no cycle
}
public ListNode findMiddle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow; // middle when fast hits the end
}
}
Common Mistakes & Pitfalls
- Only mentioning cycle detection: also mention finding the middle node to show breadth.
- Not checking
fast.next: you must check bothfast != nullandfast.next != nullbeforefast.next.next, or you get aNullPointerException. - Not explaining why they meet: state the relative-speed argument — the gap shrinks by one per step.
- Confusing the cycle-start phase: after detection, reset slow to head and move both one step at a time.
Check your understanding
Answer these without looking back at the article.
- How do you find the length of the cycle once you have detected it?
- How is slow-fast pointer used in the 'happy number' problem?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.