120. Clone Graph

MediumGraph

Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. The graph is represented as an adjacency list: a list of lists where `adjList[i]` contains the 1-indexed neighbors of node `i + 1`. For example, `[[2,4],[1,3],[2,4],[1,3]]` means node 1 connects to nodes 2 and 4, node 2 connects to nodes 1 and 3, and so on. Return the adjacency list of the cloned graph (same structure as the input). If the input is an empty list `[]`, return `[]`.

Examples

Input: [[2,4],[1,3],[2,4],[1,3]]

Output: [[2,4],[1,3],[2,4],[1,3]]

Explanation: 4-node graph: 1-2, 2-3, 3-4, 4-1. The clone has the same adjacency structure.

Constraints

  • 0 <= number of nodes <= 100
  • 1 <= Node.val <= 100
  • Node.val is unique for each node
  • No repeated edges or self-loops
  • The graph is connected and undirected
Loading...

Run checks all cases above. Submit evaluates all test cases.