2Brute2Best
🔥0

Clone Graph

Medium·LeetCode #133 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given a reference to one node of a connected, undirected graph. Every node has an integer value and a list of neighbours.

Return a deep copy of the whole graph. Every node must be copied exactly once. The copy must have the same values and the same links as the original, but it must share no node with the original. Editing the copy must never change the input.

Input

A reference node to one node of the graph, or null for an empty graph. Each node has val and a list neighbors.

Output

A reference to the copy of node in the new graph, or null if the input is null.

Example 1
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
Output: the same graph, built from new nodes

Node 1 is linked to 2 and 4, node 2 to 1 and 3, node 3 to 2 and 4, and node 4 to 1 and 3. The copy has the same four links, on four new nodes.

Example 2
Input: adjList = [[]]
Output: one new node with no links

A single node with no neighbours. The copy is one new node with an empty neighbour list.

Constraints
  • • The number of nodes is in the range [0, 100].
  • • 1 ≤ Node.val ≤ 100
  • • Node.val is unique for each node.
  • • There are no repeated edges and no self-loops.
  • • The graph is connected, and every node can be reached from the given node.
02

Pattern Recognition Clues

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

“deep copy of the whole graph”
→ Every node is copied once and the links are rebuilt between the copies. A traversal visits each node once.
“connected, undirected graph”
→ The traversal reaches every node from the start, so one search from one node finds all of them.
“node may be reached again”
→ Cycles exist, so the search needs a memory of which nodes it has seen, here a map.
03

Approach Complexity Ladder

Brute Force — search a list for each neighbour's copyO(V · E)O(V)

Collect all nodes in a list, make one copy per node, then find each neighbour's copy by a linear search

Optimal — one BFS with a map from original to copyO(V + E)O(V)

Create each copy the first time its node is seen; the map finds an existing copy in one lookup

Brute Force Intuition

First collect every original node in one list with a BFS. Make one new node for each entry. To link the copies, look at each neighbour of an original node and find its copy by scanning the list from the front, then add that copy to the matching copy's neighbour list.

Recognition rule for this sub-pattern:

Adjacency list + visited set. Count how many times you must start a new traversal.
Remember this · Graph BFS / DFS
Trigger
Grid of cells, islands, regions, "minimum steps", transformations between states, connected components.
Think
What is a node, what is an edge, and do I need the shortest path or just reachability?
Key question
How do I mark visited so I never process a node twice?
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 Graph BFS / DFS translate into production architectures and low-level memory mechanics.