Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

adjList =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Deep copy a graph using DFS + a hash map from original node to cloned node.

Approach

DFS from the start node:

  • If node already cloned, return clone.
  • Otherwise create clone, store it, then clone all neighbors recursively.

Why it works

The map prevents infinite loops on cycles and ensures shared neighbors remain shared in the clone.

Code
Loading...
Complexity
Time: O(V+E)
Space: O(V)
Solution
Updated: 2026-02-23

Idea

Deep copy a graph using DFS + a hash map from original node to cloned node.

Approach

DFS from the start node:

  • If node already cloned, return clone.
  • Otherwise create clone, store it, then clone all neighbors recursively.

Why it works

The map prevents infinite loops on cycles and ensures shared neighbors remain shared in the clone.

Code
Loading...
Complexity
Time: O(V+E)
Space: O(V)