2Brute2Best
🔥0

Find Length of Loop

Easy·GeeksforGeeks Pro OnlySlight Variation
GeeksforGeeks
01

Problem Statement

You are given head, the first node of a singly linked list. The list may contain a loop: the tail's next may point back to some earlier node.

If there is a loop, return how many nodes are in it. If there is no loop, return 0.

Write int countNodesInLoop(ListNode* head). GFG calls its node type Node (with data and next); here we use LeetCode's ListNode (val, next) — the logic is identical.

c in the examples is only used to build the test list: the tail links to the c-th node (1-based), and c = 0 means no loop. Your function does not receive it.

Input

head — the first node of the list.

Output

The number of nodes in the loop, or 0 if there is no loop.

Example 1
Input: head = 25 → 14 → 19 → 33 → 10 → 21 → 39 → 90 → 58 → 45, c = 4
Output: 7

The tail (45) links to the 4th node (33). The loop is 33 → 10 → 21 → 39 → 90 → 58 → 45 → 33, which has 7 nodes.

Example 2
Input: head = 5 → 4, c = 0
Output: 0

There is no loop.

Example 3
Input: head = 0 → 1 → 2 → 3, c = 1
Output: 4

The tail links back to the head, so all 4 nodes are in the loop.

Constraints
  • • 1 ≤ number of nodes ≤ 10^5
  • • 1 ≤ node value ≤ 10^4
  • • 0 ≤ c ≤ number of nodes
  • • Expected: O(N) time, O(1) extra space
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“detect a loop”
→ Linked list + loop → Floyd's slow/fast check.
“length of the loop”
→ After detecting it, measure it: walk one lap from a node that is surely on the loop.
“O(1) extra space”
→ No map of visited nodes.
03

Approach Complexity Ladder

Brute Force — remember the step of every nodeO(N)O(N)

Map node → step index; length = current step − first step

Optimal — Floyd meet, then walk one lapO(N)O(1)

Find a node inside the loop with slow/fast, then count one lap from it

Brute Force Intuition

Walk the list counting steps, and store each node's pointer with the step at which we first reached it. The first time we reach a node that is already in the map, we have just finished one lap of the loop, so the loop length is step − firstStep[node]. If we reach null, there is no loop.

Recognition rule for this sub-pattern:

Fast moves 2, slow moves 1. If there is a loop, fast catches slow. Reset one to head to find the loop start.
Remember this · Linked List — Fast & Slow
Trigger
Cycle detection, middle of list, "n-th from end", two lists meeting, repeating sequences.
Think
If one pointer moves faster or starts earlier, where will they meet?
Key question
What speed or gap makes the meeting point tell me the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Fast & Slow translate into production architectures and low-level memory mechanics.