Leetcode 138. Copy List with Random Pointer
A linked list is given where each node contains an additional random pointer which could point to any node in the list or null
.
You need to make a deep copy of this 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 pointers 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 a node in the original list.
For example, if there are two nodes in the original list and their random pointers point to each other, then the same should be true for the deep copy.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = nullptr;
random = nullptr;
}
};
To create a deep copy of the list, we can use a three-step approach:
// Definition for a Node.
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;
// Step 1: Create a new node for each original node and insert it between the original node and its next node.
Node* cur = head;
while (cur) {
Node* newNode = new Node(cur->val);
newNode->next = cur->next;
cur->next = newNode;
cur = newNode->next;
}
// Step 2: Assign random pointers for the new nodes.
cur = head;
while (cur) {
if (cur->random) {
cur->next->random = cur->random->next;
}
cur = cur->next->next;
}
// Step 3: Separate the original and deep-copied list.
Node* newHead = head->next;
cur = head;
while (cur) {
Node* temp = cur->next;
cur->next = temp->next;
if (temp->next) {
temp->next = temp->next->next;
}
cur = cur->next;
}
return newHead;
}
};
The time complexity of this solution is O(n), where n is the number of nodes in the original list. This is because we traverse the list multiple times but each step is linear.
Therefore, the overall time complexity is O(n).
The space complexity of this solution is O(1) additional space, not counting the space required for the new nodes. This is because we don’t use any extra data structures that grow with the size of the input list.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?