Fast and Slow Pointer Technique in Linked List: Cycle Detection, Proof, and Complete Explanation
Fast and Slow Pointer Technique in Linked List: Cycle Detection, Proof, and Complete Explanation

Fast and Slow Pointer Technique in Linked List: Cycle Detection, Proof, and Complete Explanation

Why Fast Moves 2 Steps? Why They Meet? How Cycle Start Is Found? — Everything Explained Simply + Deeply

🚀 Before We Start

Try these problems (optional but helpful):

  1. https://leetcode.com/problems/linked-list-cycle/
  2. https://leetcode.com/problems/linked-list-cycle-ii/


🤔 Let’s Talk Honestly…

When you first learn this technique, you’re told:

👉 “Slow moves 1 step, fast moves 2 steps”

👉 “If they meet → cycle exists”

But your brain asks:

❓ Why 2 steps?

❓ Why do they meet at all?

❓ Why does resetting pointer find cycle start?

❓ Is this magic or logic?

👉 Let’s answer each doubt one by one.


🧩 Doubt 1: Why do we even use two pointers?

❓ Question:

Why not just use one pointer?

✅ Answer:

With one pointer:

  1. You can only move forward
  2. You cannot detect loops efficiently

👉 Two pointers create a relative motion

That relative motion is the key.


🧩 Doubt 2: Why fast = 2 steps and slow = 1 step?

❓ Question:

Why exactly 2 and 1?

✅ Answer:

We need:

Fast speed > Slow speed

So that:

👉 Fast catches up to slow

🧠 Think like this:

If both move same speed:

Slow → 1 step
Fast → 1 step

👉 They will NEVER meet ❌

If:

Slow → 1 step
Fast → 2 steps

👉 Fast gains 1 node every step

🔥 Key Insight:

Relative speed = fast - slow = 1

👉 This means fast is closing the gap by 1 node every step


🧩 Doubt 3: Why do they ALWAYS meet in a cycle?

❓ Question:

Okay, fast is faster… but why guaranteed meeting?

🧠 Imagine a Circular Track

Inside a cycle, the list behaves like:

Circle of length = λ

Now:

  1. Slow moves 1 step
  2. Fast moves 2 steps

🔄 Gap Behavior

Each step:

Gap = Gap - 1

Because fast is catching up.

Eventually:

Gap = 0

👉 They meet 🎯

💡 Simple Analogy

Two runners on a circular track:

  1. One is faster
  2. One is slower

👉 Faster runner will lap and meet slower runner


🧩 Doubt 4: What if there is NO cycle?

❓ Question:

Why does this fail without cycle?

✅ Answer:

If no cycle:

List ends → fast reaches null

👉 No loop → no meeting


🧩 Doubt 5: Where do they meet?

❓ Question:

Do they meet at cycle start?

❌ Answer:

No, not necessarily.

They meet somewhere inside the cycle


🧩 Doubt 6: Then how do we find the cycle start?

Now comes the most important part.

🎯 Setup

Let’s define:

a = distance from head to cycle start
b = distance from cycle start to meeting point
c = remaining cycle

Cycle length:

λ = b + c

🧠 What happens when they meet?

Slow distance:

a + b

Fast distance:

2(a + b)

Using relation:

2(a + b) = a + b + kλ

Solve:

a + b = kλ
=> a = kλ - b
=> a = (k-1)λ + (λ - b)


💥 Final Meaning

a = distance from meeting point to cycle start


🔥 BIG CONCLUSION

👉 Distance from head → cycle start

👉 = Distance from meeting point → cycle start


🧩 Doubt 7: Why resetting pointer works?

❓ Question:

Why move one pointer to head?

✅ Answer:

Because:

  1. One pointer is a away from start
  2. Other is also a away (via cycle)

👉 Move both 1 step:

They meet at:

Cycle Start 🎯


🔄 Visualization

Head → → → Cycle Start → → Meeting Point → → back to Start

Both pointers:

  1. One from head
  2. One from meeting point

👉 Same distance → meet at start


🧩 Doubt 8: Can we use fast = 3 steps?

❓ Question:

Will this work?

✅ Answer:

Yes, BUT:

  1. Math becomes complex
  2. Harder to reason
  3. No extra benefit

👉 So we use simplest:

2 : 1 ratio


🧠 Final Mental Model

Think in 3 steps:

1. Different Speeds

Fast moves faster → gap reduces

2. Circular Structure

Cycle → positions repeat

3. Guaranteed Meeting

Finite positions + relative motion → collision


🧩 TEMPLATE 1: Detect Cycle

ListNode slow = head;
ListNode fast = head;

while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;

if(slow == fast){
return true;
}
}
return false;


🧩 TEMPLATE 2: Find Cycle Start

ListNode slow = head;
ListNode fast = head;

while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;

if(slow == fast){
slow = head;

while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;


🧩 TEMPLATE 3: Find Middle of Linked List

❓ Problem

Find the middle node of a linked list.

🧠 Intuition

Fast moves twice as fast:

When fast reaches end → slow reaches half

👉 Slow = middle


💻 Code

ListNode slow = head;
ListNode fast = head;

while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}

return slow;


⚠️ Even Length Case

1 → 2 → 3 → 4 → 5 → 6

👉 Returns 4 (second middle)


❓ How to Get First Middle?

while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;


🧩 Where Else This Technique Is Used?

  1. Detect cycle
  2. Find cycle start
  3. Find middle node
  4. Check palindrome (linked list)
  5. Split list (merge sort)
  6. Intersection problems


⚙️ Time & Space Complexity

Time: O(n)
Space: O(1)


❌ Common Mistakes

  1. Forgetting fast.next != null
  2. Thinking meeting point = cycle start ❌
  3. Not resetting pointer properly


🧠 Final Mental Model

Think in 3 steps:

1. Speed Difference

Fast moves faster → gap reduces

2. Circular Nature

Cycle → repeated positions

3. Guaranteed Meeting

Finite nodes + relative motion → collision


🔥 One Line to Remember

Fast catches slow because it reduces the gap inside a loop.


🚀 Conclusion

Now you understand:

✅ Why fast moves faster

✅ Why they meet

✅ Why meeting proves cycle

✅ Why reset gives cycle start

✅ How to find middle using same logic


👉 This is not just a trick…

It’s a mathematical guarantee based on motion and cycles.


💡 Master this once, and you’ll solve multiple linked list problems easily.

Ai Assistant Kas