138. Copy List with Random Pointer
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for a deep copy of a linked list where each node has a val, a next pointer, and a random pointer. A deep copy means creating an entirely new list that replicates the structure and values of the original. The key challenge is to correctly wire up the random pointers in the new list. When we copy a node, its random pointer might point to a node that we haven't created a copy for yet, or one that's far down the list. We need a way to map original nodes to their corresponding new nodes efficiently.
Solution
Copy List with Random Pointer in Python
"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head:
return None
# Pass 1: Create a copy of each node and interweave them.
# Original: A -> B -> C
# After: A -> A' -> B -> B' -> C -> C'
curr = head
while curr:
new_node = Node(curr.val, curr.next)
curr.next = new_node
curr = new_node.next
# Pass 2: Assign random pointers for the copied nodes.
curr = head
while curr:
if curr.random:
# The copied node is curr.next.
# Its random should point to the copy of curr.random,
# which is curr.random.next.
curr.next.random = curr.random.next
# Move to the next original node.
curr = curr.next.next
# Pass 3: Separate the original and copied lists.
original = head
new_head = head.next
clone = new_head
while original:
original.next = clone.next
original = original.next
if original:
clone.next = original.next
else:
clone.next = None
clone = clone.next
return new_head
Copy List with Random Pointer in Java
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
// Pass 1: Create a copy of each node and interweave them.
// Original: A -> B -> C
// After: A -> A' -> B -> B' -> C -> C'
Node curr = head;
while (curr != null) {
Node newNode = new Node(curr.val);
newNode.next = curr.next;
curr.next = newNode;
curr = newNode.next;
}
// Pass 2: Assign random pointers for the copied nodes.
curr = head;
while (curr != null) {
if (curr.random != null) {
// The copied node is curr.next.
// Its random should point to the copy of curr.random,
// which is curr.random.next.
curr.next.random = curr.random.next;
}
// Move to the next original node.
curr = curr.next.next;
}
// Pass 3: Separate the original and copied lists.
Node original = head;
Node newHead = head.next;
Node clone = newHead;
while (original != null) {
original.next = clone.next;
original = original.next;
if (original != null) {
clone.next = original.next;
} else {
clone.next = null;
}
clone = clone.next;
}
return newHead;
}
}
Copy List with Random Pointer in C++
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
Node* copyRandomList(Node* head) {
if (!head) {
return nullptr;
}
// Pass 1: Create a copy of each node and interweave them.
// Original: A -> B -> C
// After: A -> A' -> B -> B' -> C -> C'
Node* curr = head;
while (curr) {
Node* newNode = new Node(curr->val);
newNode->next = curr->next;
curr->next = newNode;
curr = newNode->next;
}
// Pass 2: Assign random pointers for the copied nodes.
curr = head;
while (curr) {
if (curr->random) {
// The copied node is curr->next.
// Its random should point to the copy of curr->random,
// which is curr->random->next.
curr->next->random = curr->random->next;
}
// Move to the next original node.
curr = curr->next->next;
}
// Pass 3: Separate the original and copied lists.
Node* original = head;
Node* newHead = head->next;
Node* clone = newHead;
while (original && clone) {
original->next = clone->next;
original = original->next;
if (original) {
clone->next = original->next;
} else {
clone->next = nullptr;
}
clone = clone->next;
}
return newHead;
}
};
Copy List with Random Pointer in TypeScript
/**
* Definition for _Node.
* class _Node {
* val: number
* next: _Node | null
* random: _Node | null
*
* constructor(val?: number, next?: _Node, random?: _Node) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* this.random = (random===undefined ? null : random)
* }
* }
*/
function copyRandomList(head: _Node | null): _Node | null {
if (!head) {
return null;
}
// Pass 1: Create a copy of each node and interweave them.
// Original: A -> B -> C
// After: A -> A' -> B -> B' -> C -> C'
let curr: _Node | null = head;
while (curr) {
const newNode = new _Node(curr.val, curr.next, null);
curr.next = newNode;
curr = newNode.next;
}
// Pass 2: Assign random pointers for the copied nodes.
curr = head;
while (curr) {
if (curr.random) {
// The copied node is curr.next.
// Its random should point to the copy of curr.random,
// which is curr.random.next.
curr.next!.random = curr.random.next;
}
// Move to the next original node.
curr = curr.next!.next;
}
// Pass 3: Separate the original and copied lists.
let original: _Node | null = head;
const newHead = head.next!;
let clone: _Node | null = newHead;
while (original) {
original.next = clone.next;
original = original.next;
if (original) {
clone.next = original.next;
} else {
clone.next = null;
}
clone = clone.next;
}
return newHead;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.