Clone Graph
Part of Practice: Flood Fill & Regions
You get a reference to one node of a connected, undirected graph. Each node has an integer value and a list of neighbor nodes. Return a deep copy of the whole graph: brand-new node objects with the same values and the same connection structure, sharing no objects with the original. The graph has up to 100 nodes, values are unique, and there are no self-loops or duplicate edges. If the input node is null, return null.
Examples
- Input:
- adjacency list = [[2,4],[1,3],[2,4],[1,3]] (node 1 connects to 2 and 4, node 2 to 1 and 3, ...)
- Output:
- [[2,4],[1,3],[2,4],[1,3]]
The returned graph has the identical shape — a 4-node cycle — but every node object is newly created. Returning the original node would be wrong even though it prints the same.
- Input:
- adjacency list = [[]]
- Output:
- [[]]
A single node with no neighbors: the copy is one new node with an empty neighbor list.
- Input:
- adjacency list = []
- Output:
- []
The input reference is null (no graph), so the answer is null.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.