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.valrandom_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]]
Constraints:
0 <= n <= 1000-104 <= Node.val <= 104Node.random is null or is pointing to some node in the linked list.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a complex network of people connected by two types of threads, and you need to build an exact duplicate of the entire group. The brute force approach involves creating all the new people first, and then exhaustively searching through the original group to figure out how every single connection should be re-attached for the new copies.
Here's how the algorithm would work step-by-step:
class Node:
def __init__(self, val=0, next=None, random=None):
self.val = val
self.next = next
self.random = random
def copyRandomList(head):
if not head:
return None
# We first generate all nodes without connections so they exist in memory to reference later
original_nodes = []
copied_nodes = []
current_original = head
while current_original:
original_nodes.append(current_original)
copied_nodes.append(Node(current_original.val))
current_original = current_original.next
# We iterate through the original list again to find index positions for every pointer target
for original_index in range(len(original_nodes)):
target_next = original_nodes[original_index].next
if target_next:
next_target_index = original_nodes.index(target_next)
copied_nodes[original_index].next = copied_nodes[next_target_index]
target_random = original_nodes[original_index].random
if target_random:
random_target_index = original_nodes.index(target_random)
copied_nodes[original_index].random = copied_nodes[random_target_index]
# We return the head of the newly created copy list since all structural threads are fully wired
return copied_nodes[0]To duplicate a linked list with extra random connections, the best method avoids getting confused by trying to set up all the random links at the same time. Instead, it cleverly weaves the cloned items right next to their original counterparts so that finding the correct random destination becomes completely obvious.
Here's how the algorithm would work 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
current_node = head
while current_node:
cloned_node = Node(current_node.val)
cloned_node.next = current_node.next
current_node.next = cloned_node
current_node = cloned_node.next
# Intertwine clones next to originals to easily map out random pointers. current_node = head
while current_node:
if current_node.random:
current_node.next.random = current_node.random.next
current_node = current_node.next.next
# Assign random pointers to the cloned nodes using the interleaved structure. current_node = head
cloned_head = head.next
while current_node:
cloned_node = current_node.next
current_node.next = cloned_node.next
current_node = current_node.next
if cloned_node.next:
cloned_node.next = cloned_node.next.next
current_node = current_node.next
# Restore the original list and separate the cloned list entirely.
return cloned_head| Case | How to Handle |
|---|---|
| Null head pointer representing an empty list | Return null immediately since there are no nodes to copy. |
| Linked list with a single node whose random pointer points to itself | The node creation logic maps the single node to its copy and assigns its random pointer to reference the copied node. |
| Linked list with a single node whose random pointer is null | The algorithm creates the cloned node with its next and random pointers correctly set to null. |
| Nodes with random pointers pointing backwards to earlier nodes in the list | A hash map or interweaving technique ensures previously created nodes are accessible when resolving random pointers. |
| Nodes with random pointers pointing forwards to later nodes not yet visited | A multi-pass approach or map-based lookup guarantees forward references are established once all nodes are instantiated. |
| All nodes having identical integer values | Using node object references as keys in the hash map instead of node values prevents collisions and ambiguity. |
| Extremely long linked lists causing deep recursion stacks | Iterative approaches or careful pointer manipulation are used to prevent stack overflow errors common in recursive deep copies. |
| Random pointers forming complex cyclical references among multiple nodes | The mapping structure correctly resolves cycles by looking up existing clones regardless of traversal order. |