Linked List Cycle II
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.
head — the first node of the list (it can be null).
The node where the cycle starts, or null if there is no cycle.
The tail links back to index 1, so the walk 3 → 2 → 0 → -4 → 2 → … first repeats at the node holding 2.
The tail links back to the head, so the cycle starts at the head.
No cycle.
- • 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?
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Hash set of node pointers; return the first node already in the set
Meet with slow/fast, then step from head and from the meeting point together
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Fast & Slow translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.