138. Copy List with Random Pointer
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Linked List
- Time
- O(N)
- Space
- O(1)
Standard Solution (C++)
SummaryGiven a linked list where each node has both a `next` pointer and a `random` pointer (which can point to any node or null), produce a fully independent deep copy of the entire list, returning the head of the cloned list.
Approach
- Interleave clones into the original list: for each original node, insert a new clone node immediately after it, giving the layout
old1 -> new1 -> old2 -> new2 -> .... - Wire random pointers: because
old.random->nextis exactly the clone ofold.random, each clone's random can be set in a single pass with no extra data structure. - Separate the two lists: unlink the interleaved clones back into their own list while restoring the original list, then return the clone head.
- This approach runs in O(n) time and O(1) auxiliary space (no hash map needed).
#include <cstddef>
using namespace std;
// Node definition as given by LeetCode
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) : val(_val), next(nullptr), random(nullptr) {}
};
class Solution {
public:
Node* copyRandomList(Node* head) {
if (!head) return nullptr;
// Phase 1: interleave clones.
// Transform: A -> B -> C into A -> A' -> B -> B' -> C -> C'
Node* cur = head;
while (cur) {
Node* clone = new Node(cur->val);
clone->next = cur->next;
cur->next = clone;
cur = clone->next; // advance to the original next
}
// Phase 2: assign random pointers to clones.
// cur->next is the clone of cur; cur->random->next is the clone of random.
cur = head;
while (cur) {
if (cur->random)
cur->next->random = cur->random->next;
cur = cur->next->next; // skip over clone to next original
}
// Phase 3: separate the interleaved list into original and clone lists.
Node dummy(0);
Node* tail = &dummy;
cur = head;
while (cur) {
Node* clone = cur->next;
tail->next = clone;
tail = clone;
cur->next = clone->next; // restore original list
cur = cur->next;
}
return dummy.next;
}
};
Time O(n) Space O(1)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (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;
}
// old1, old2, ...
// old1, new1, old2, new2, ...
// copy next
Node* cur=head;
while(cur){
Node* newNode=new Node(cur->val);
newNode->next=cur->next;
cur->next=newNode;
cur=newNode->next;
}
// copy random
cur=head;
while(cur){
if(cur->random){
cur->next->random=cur->random->next;
}
cur=cur->next->next;
}
// separate the new list
Node dummy=Node(0);
Node* ccur=&dummy;
cur=head;
while(cur){
ccur->next=cur->next;
ccur=ccur->next;
cur->next=cur->next->next;
cur=cur->next;
}
return dummy.next;
}
};
// Time Complexity: O(N)
// Space Complexity: O(1), since we use interleaving nodes to avoid using extra space for a hash map. Time O(N) Space O(1)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground C++
Your solution. A driver couldn't be auto-generated for this signature — add a main() that calls the method and prints the result, then Run.
C++ ▶ Runnable Couldn't auto-generate a driver for this signature — edit to add one.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.