Copy List With Random Pointer

Copy List With Random Pointer Problem

You are given the head of a linked list of length n, where each node has an extra pointer called random, which can point to any node in the list or be null.

Your task is to create a deep copy of the list, ensuring the new list consists of exactly n nodes with the following:

  • Each node should have the same value as the original.
  • next pointer in the copied node should correspond to the next pointer of the original.
  • random pointer in the copied node should correspond to the random pointer of the original.
Copy List With Random Pointer Problem

Important Facts for Copy List With Random Pointer

Example 1:

Copy List With Random Pointer Example 1

Example 2:

Copy List With Random Pointer Example 2

Constraints:

  • 0 <= n <= 100
  • -100 <= Node.val <= 100
  • random is null or is pointing to some node in the linked list.

Hints to Solve Copy List With Random Pointer

Hint 1 :

  • Each node has an additional random pointer that can point to any node in the list.

  • Unlike the next pointer, the random pointer doesn’t follow a fixed sequence. To create a deep copy, all nodes and pointers must be recreated separately.

  • Why can’t we build the new list in a single iteration?

Hint 2 :

  • When copying nodes, we can’t immediately assign random pointers since they may point to nodes that haven’t been copied yet.
  • Possible solution is to create copies of all nodes in one pass while storing the mapping between the original and copied nodes.
  • Can you think of a data structure to manage this mapping effectively?

Hint 3 :

  • Using a hash map can efficiently map each original node to its copy, allowing quick retrieval in O(1) time.
  • In the first pass, create node copies and store them in the map. In the second pass, use the map to assign random pointers to the copied nodes.

Recommendation for Time and Space Complexity (For Copy List With Random Pointer)

Aim for a solution with O(n) time and O(n) space, where n is the length of the list.

There are mainly 5 approach to solve Copy List With Random Pointer problem:

  1. Hash Map (Recursion)
  2. Hash Map (Two Pass)
  3. Hash Map (One Pass)
  4. Space Optimized – I
  5. Space Optimized – II

1. Hash Map (Recursion)

Use a hash map to store copies of nodes as you recursively traverse the original list. If a node is visited, fetch its copy from the map; otherwise, create a new copy and continue until the entire list is cloned.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

2. Hash Map (Two Pass)

In the first pass, traverse the original list and create a copy of each node while storing the mapping in a hash map. In the second pass, assign next and random pointers to the copied nodes using the map.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

3. Hash Map (One Pass)

Traverse the list once and simultaneously create copies of nodes, storing them in a hash map. As you create the copy, assign the next and random pointers directly using the map.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

4. Space Optimized – I

Without using extra space, interweave copied nodes with original nodes in a single list. Assign the next and random pointers in two steps, then separate the original and copied nodes.

  • Time complexity: O(n)
  • Space complexity: O(1)

Code

5. Space Optimized – II

Modify the interweaving approach to assign next and random pointers in a single traversal while separating the original and copied lists. This approach minimizes passes and maintains O(1) space.

  • Time complexity: O(n)
  • Space complexity: O(1)

Code

More Articles