LeetCode 133 · Medium
Clone Graph Explained (LeetCode 133)
Updated Dec 4, 2026 · 4 min read
Walk the graph with DFS and keep a map from each original node to its copy. When you reach a node, return its copy if the map already has one; otherwise make the copy, store it in the map right away, then clone each neighbor and append it. Every node is copied once, so it runs in O(V + E) time and O(V) space.

Key points
- ●A plain copy of a node and its neighbors loops forever as soon as the graph has a cycle.
- ●A map from original node to copy answers one question: did I already make this one?
- ●Store the copy in the map before you visit its neighbors, or a back edge loops again.
- ●Each node is copied once and each edge is checked from both ends: O(V + E) time, O(V) space.

The riddle: Leo's copy never ends
Leo is redrawing his friend group on a new whiteboard. Four friends sit in a ring: 1, 2, 3, 4. Each one knows the two people beside them, so 1 knows 2 and 4, and 2 knows 1 and 3. The rule: new sticky notes only, no reusing the old ones.
He copies 1. Then 1's friends, 2 and 4. Then 2's friends, 1 and 3. Wait, 1 again? He makes another 1, then another 2, and the board fills up with notes for the same four people. That is the trap in LeetCode 133, Clone Graph: you get one node of a connected, undirected graph, where each node has a value and a list of neighbors, and you return a deep copy. New nodes, the same values, the same connections, and no new node pointing at an old one.
Why the obvious copy breaks
Try it on tiny inputs first. No node at all: return nothing. One node with no neighbors: one new node, done. Two nodes joined, 1 and 2: copy 1, its neighbor is 2, copy 2. But 2's neighbor is 1, and if you copy blindly you make a second 1, which points to 2 again, and so on with no end.
The problem is the cycle. Any cycle sends you back to where you started, and a graph here can have up to 100 nodes, so there are plenty of ways back.
The idea: one question at every node
Before copying a node, ask: did I already make your copy? Keep a map called clones from each original node to its copy.
- If the node is in the map, hand back the copy that is already there.
- If not, make a new node with the same value, write it into the map, and only then copy its neighbors.
Step 3 of the visual sheet above calls the map the "cycle stop", and Step 4 is the rule that makes it work: create, store, then recurse.
Trace it on the square
The graph is the ring 1-2, 2-3, 3-4, 4-1, with adjacency list [[2,4],[1,3],[2,4],[1,3]].
| Visit | Map hit? | What happens | Map after |
|---|---|---|---|
| 1 | no | make 1' | 1 |
| 2 (from 1) | no | make 2' | 1, 2 |
| 1 (from 2) | yes | reuse 1', link 2'-1' | 1, 2 |
| 3 (from 2) | no | make 3' | 1, 2, 3 |
| 2 (from 3) | yes | reuse 2' | 1, 2, 3 |
| 4 (from 3) | no | make 4' | 1, 2, 3, 4 |
| 1, 3 (from 4) | yes, yes | reuse 1' and 3' | 1, 2, 3, 4 |
| 4 (from 1) | yes | reuse 4' | 1, 2, 3, 4 |
Four new nodes and eight neighbor links: the square has 4 edges and each one is stored from both ends. The output adjacency list matches the input, but every node object is new.
The order matters
Store the copy in the map before you loop over the neighbors. If you store it after, node 2 asks the map for node 1 while node 1 is still being built, finds nothing, and the loop is back. Storing first means the back edge finds the partly built copy and links to it. The sheet's "Early insertion is the key" box says the same thing.
Python solution
class Node:
def __init__(self, val=0, neighbors=None):
self.val = val
self.neighbors = neighbors or []
def cloneGraph(node):
clones = {}
def clone(u):
if u in clones:
return clones[u]
copy = Node(u.val)
clones[u] = copy # store before visiting neighbors
for v in u.neighbors:
copy.neighbors.append(clone(v))
return copy
return clone(node) if node else None
On the square, clone(1) creates 1', 2', 3' and 4' once each and returns 1', whose neighbors are 2' and 4'.
Complexity
Without the map, one cycle means the copy runs forever. With it, each node is created once and each edge is checked twice, so the time is O(V + E). The map holds one entry per node and the recursion can go as deep as the number of nodes, so the space is O(V).
The trap to remember
Two mistakes catch people. The first is copying without a map, which loops on any cycle. The second is filling the map too late, after the neighbor loop, which loops in exactly the same way. Guard the empty input, check the map, create and store, clone the neighbors, return the copy.
Frequently asked questions
Why does Clone Graph need a hash map?
Because an undirected graph with a cycle leads you back to nodes you have already copied. In the square 1-2-3-4-1, copying 2 leads back to 1. The map tells you a copy of 1 already exists, so you reuse it instead of making a second one and looping forever.
Why store the clone before visiting the neighbors?
If you store it after the neighbor loop, node 2 asks the map for node 1 while node 1 is still being built, finds nothing, and starts copying 1 again. Storing first means any back edge finds the partly built copy and links to it.
Can Clone Graph be solved with BFS instead of DFS?
Yes. Copy the start node, put it in the map and a queue, then for each node you pop, create copies of unseen neighbors, add them to the map and the queue, and append the copies to the current copy's neighbor list. The map does the same job in both versions.
What is the time complexity of Clone Graph?
O(V + E). Each of the V nodes is created once, and each undirected edge is looked at twice, once from each end. The map holds V entries and the recursion can go V deep, so space is O(V).