Clone Graph
Given a reference to a node in a connected, undirected graph, return a deep copy (clone) of the entire graph. Each node holds an integer val and a list of its neighbors.
Open official problem prompt ↗Produce an independent copy of a graph whose shape matches the original exactly, including cycles.
Like redrawing a subway map on fresh paper: each station gets drawn once, and every time a line points back to a station you already drew, you connect to that existing dot rather than drawing it again.
- Input
- adjList = [[2,4],[1,3],[2,4],[1,3]]
- Output
- [[2,4],[1,3],[2,4],[1,3]]
- Why
- The clone is a brand-new set of 4 nodes with identical values and identical neighbor connections as the original.
The number of nodes is in the range [0, 100]1 <= Node.val <= 100Node.val is unique for each nodeThere are no repeated edges and no self-loopsThe graph is connected and all nodes can be visited from the given node