2Brute2Best
🔥0

Linked List Cycle II

Medium·LeetCode #142 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given head, the first node of a singly linked list. If the list has a cycle, return the node where the cycle begins — the first node you reach a second time when walking from head. If there is no cycle, return null.

Do not change the list.

pos in the examples is only used to build the test list: it is the index of the node the tail links back to (-1 means no cycle). Your function does not receive it, and the answer must be the node itself, not its value or index.

Input

head — the first node of the list (it can be null).

Output

The node where the cycle starts, or null if there is no cycle.

Example 1
Input: head = [3, 2, 0, -4], pos = 1
Output: the node at index 1 (value 2)

The tail links back to index 1, so the walk 3 → 2 → 0 → -4 → 2 → … first repeats at the node holding 2.

Example 2
Input: head = [1, 2], pos = 0
Output: the node at index 0 (value 1)

The tail links back to the head, so the cycle starts at the head.

Example 3
Input: head = [1], pos = -1
Output: null

No cycle.

Constraints
  • • The number of nodes is in the range [0, 10^4]
  • • −10^5 ≤ Node.val ≤ 10^5
  • • pos is −1 or a valid index in the list
  • • Follow-up: can you solve it using O(1) extra memory?
02

Pattern Recognition Clues

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

“return the node where the cycle begins”
→ Not just yes/no → Floyd needs a second phase.
“do not modify the list”
→ No marking nodes or breaking links to detect the repeat.
“O(1) memory follow-up”
→ No visited set → use the distance relation from slow/fast speeds.
03

Approach Complexity Ladder

Brute Force — first node seen twiceO(N)O(N)

Hash set of node pointers; return the first node already in the set

Optimal — Floyd's two phasesO(N)O(1)

Meet with slow/fast, then step from head and from the meeting point together

Brute Force Intuition

Walk from head and store each node's address in a hash set. The first time we step onto a node that is already stored, that node is the start of the cycle: every node before it on the walk was new, and this is the first place the walk comes back to.

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.