Facebook Pixel

Clone Graph

You are given a connected undirected graph and must return a deep copy (clone) of it — a new graph with the same nodes and the same edges, built independently of the input.

The graph is described by an adjacency list adjList, where adjList[i] lists the neighbors of the i-th node. Return the clone in the same adjacency-list form.

Input & Output
Input
adj_list — An adjacency list representing the graph where adjList[i] contains the indices of neighbors of node i.
Output
A deep copy of the graph represented as an adjacency list with the same structure as the input.
Example
Input
adjList = [[2,4],[1,3],[2,4],[1,3]]
Output
[[2,4],[1,3],[2,4],[1,3]]
Constraints
  • The number of nodes in the graph is in the range [0, 100]
  • 1 <= Node.val <= 100
  • Node.val is unique for each node
  • There are no repeated edges and no self-loops in the graph
  • The Graph is connected and all nodes can be visited starting from the given node

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro