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

Krishna Shrivastava
51 views
LinkedInGithubX
0
0
Fast and Slow Pointer Technique in Linked List: Cycle Detection, Proof, and Complete Explanation
Listen to articleAudio version
Ad

πŸš€ 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