Clone Graph

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Clone Graph

Approach

Clone each node once and store it in a map keyed by node value. DFS edges, creating clone + wiring neighbors recursively.

Complexity Analysis

Time
O(V + E)
Space
O(V)

Pattern

Graph Copy with Memoization

Why It Works

The map guarantees one clone per original node and prevents infinite recursion in cycles while still reconstructing the full edge structure.

Updated Feb 2026