Find Length of Loop in Linked List — Complete Guide with Intuition, Dry Run & Floyd’s Cycle Algorithm
Find Length of Loop in Linked List — Complete Guide with Intuition, Dry Run & Floyd’s Cycle Algorithm

Find Length of Loop in Linked List — Complete Guide with Intuition, Dry Run & Floyd’s Cycle Algorithm

Detect Loop and Calculate Its Length Efficiently Using Fast & Slow Pointer Technique

5 views
0
0

🔗 Try This Problem

Practice here:

https://practice.geeksforgeeks.org/problems/find-length-of-loop/1

📌 Problem Overview

You are given the head of a singly linked list.

Your task is:

  1. Detect whether a loop (cycle) exists
  2. If a loop exists → return the length of the loop
  3. If no loop exists → return 0

🧠 Understanding the Problem

A loop in a linked list means:

👉 A node’s next pointer is pointing to a previous node, forming a cycle.

Example

1 → 2 → 3 → 4 → 5
↑ ↓
← ← ← ←

Here:

  1. Loop starts at node 3
  2. Loop nodes = 3 → 4 → 5
  3. Loop length = 3

💡 Intuition — How to Think About This Problem

There are two main challenges:

🔹 1. Detect if a loop exists

🔹 2. Find the length of that loop

🤔 Brute Force Thinking

  1. Store visited nodes in a HashSet
  2. If node repeats → loop detected

✔ Works

❌ Extra space O(n)

🚀 Optimized Thinking (Floyd’s Cycle Detection)

We use:

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

🧠 Key Idea

👉 If a loop exists:

  1. Fast pointer will eventually meet slow pointer

👉 If no loop:

  1. Fast pointer reaches null

🎥 Visual Intuition & Dry Run

⚙️ Optimized Approach (Step-by-Step)

Step 1: Detect Loop

  1. Initialize:
slow = head
fast = head
  1. Move:
slow → 1 step
fast → 2 steps
  1. If:
slow == fast → loop exists

Step 2: Find Length of Loop

Once loop is detected:

  1. Keep one pointer fixed
  2. Move another pointer until it comes back
  3. Count steps

Why This Works?

Because:

  1. Inside a loop, traversal becomes circular
  2. So eventually, we will return to the same node

🧪 Dry Run (Manual Understanding)

Example:

Loop: 3 → 4 → 5 → 3

Step 1: Detect Loop

  1. Slow and fast meet inside loop

Step 2: Count Loop Length

Start from meeting point:

Move one pointer:
3 → 4 → 5 → 3

Count = 3

💻 Code (Java)

class Solution {
public int lengthOfLoop(Node head) {

Node slow = head;
Node fast = head;

// Step 1: Detect loop
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;

if(fast == slow){

// Step 2: Count loop length
int length = 1;
Node temp = slow.next;

while(temp != slow){
temp = temp.next;
length++;
}

return length;
}
}

return 0; // No loop
}
}

⏱️ Complexity Analysis

TypeValue
Time ComplexityO(n)
Space ComplexityO(1)

🔍 Deep Insight — Why Fast Meets Slow?

  1. Fast moves twice as fast as slow
  2. Inside a loop → paths become circular
  3. Difference in speed guarantees collision

👉 This is a mathematical certainty in cyclic structures

⚠️ Edge Cases

  1. No loop → return 0
  2. Single node loop → return 1
  3. Large loop → still works efficiently

🧩 Key Takeaways

  1. Floyd’s Cycle Detection is powerful
  2. Loop detection + loop length can be done in one traversal
  3. No extra space needed

🏁 Final Thoughts

This problem is a classic linked list concept and very important for interviews.

👉 Once you master this:

  1. Detect cycle
  2. Find loop length
  3. Find loop starting node

All become easier.

Ai Assistant Kas