A linked list of length n
is given such that each node contains an additional random pointer, which could point to any node in the list, or null
.
Construct a deep copy of the list. The deep copy should consist of exactly n
brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next
and random
pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
For example, if there are two nodes X
and Y
in the original list, where X.random --> Y
, then for the corresponding two nodes x
and y
in the copied list, x.random --> y
.
Return the head of the copied linked list.
The linked list is represented in the input/output as a list of n
nodes. Each node is represented as a pair of [val, random_index]
where:
val
: an integer representing Node.val
random_index
: the index of the node (range from 0
to n-1
) that the random
pointer points to, or null
if it does not point to any node.Your code will only be given the head
of the original linked list.
Example 1:
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]] Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2:
Input: head = [[1,1],[2,1]] Output: [[1,1],[2,1]]
Example 3:
Input: head = [[3,null],[3,0],[3,null]] Output: [[3,null],[3,0],[3,null]]
Example 4:
Input: head = [] Output: [] Explanation: The given linked list is empty (null pointer), so return null.
Constraints:
0 <= n <= 1000
-10000 <= Node.val <= 10000
Node.random
is null
or is pointing to some node in the linked list.Your algorithm should run in O(n) time and use O(1) extra space.
The core challenge of this problem is to create a deep copy of a linked list where each node has an additional random pointer. The deep copy should be a completely new list with no shared nodes with the original list. This problem is significant in scenarios where data integrity and isolation are crucial, such as in undo operations, cloning complex data structures, etc.
To solve this problem, we can use a three-step approach:
Let's break down the algorithm step-by-step:
class Node:
def __init__(self, val: int, next: 'Node' = None, random: 'Node' = None):
self.val = val
self.next = next
self.random = random
def copyRandomList(head: 'Node') -> 'Node':
if not head:
return None
# Step 1: Create new nodes and interweave them with the original nodes
current = head
while current:
new_node = Node(current.val, current.next)
current.next = new_node
current = new_node.next
# Step 2: Assign random pointers for the new nodes
current = head
while current:
if current.random:
current.next.random = current.random.next
current = current.next.next
# Step 3: Restore the original list and extract the copied list
current = head
new_head = head.next
while current:
new_node = current.next
current.next = new_node.next
if new_node.next:
new_node.next = new_node.next.next
current = current.next
return new_head
The time complexity of this algorithm is O(n) because we traverse the list a constant number of times. The space complexity is O(1) extra space because we are not using any additional data structures that grow with the input size.
Consider the following edge cases:
To test the solution comprehensively, consider the following test cases:
def print_list(head):
result = []
while head:
random_index = None if not head.random else head.random.val
result.append([head.val, random_index])
head = head.next
return result
# Test case 1
head = Node(7)
node1 = Node(13)
node2 = Node(11)
node3 = Node(10)
node4 = Node(1)
head.next = node1
node1.next = node2
node2.next = node3
node3.next = node4
node1.random = head
node2.random = node4
node3.random = node2
node4.random = head
copied_head = copyRandomList(head)
print(print_list(copied_head)) # Expected: [[7, None], [13, 7], [11, 1], [10, 11], [1, 7]]
# Test case 2
head = Node(1)
node1 = Node(2)
head.next = node1
head.random = node1
node1.random = node1
copied_head = copyRandomList(head)
print(print_list(copied_head)) # Expected: [[1, 2], [2, 2]]
# Test case 3
head = Node(3)
node1 = Node(3)
node2 = Node(3)
head.next = node1
node1.next = node2
node1.random = head
copied_head = copyRandomList(head)
print(print_list(copied_head)) # Expected: [[3, None], [3, 3], [3, None]]
# Test case 4
head = None
copied_head = copyRandomList(head)
print(print_list(copied_head)) # Expected: []
When approaching such problems, consider the following tips:
In this blog post, we discussed how to solve the problem of copying a linked list with random pointers. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills and improving your coding abilities.
For further reading and practice, consider the following resources:
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE