You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Traverse the linked list and create deep copy of the current node and push both in the hashmap.
Now traverse the linked list again and point deep copied node to the other deep copy nodes as present in original node.
TC: O(N)
SC: O(N)
Optimized
3 Step algorithm explained in code.
TC: O(N)
SC: O(1)
Code
classSolution {
public:
Node* copyRandomList(Node* head)
{
Node* iter = head;
Node* front = head;
// First round: make copy of each node,// and link them together side-by-side in a single list.while (iter != NULL) {
front = iter->next;
Node* copy = newNode(iter->val);
iter->next = copy;
copy->next = front;
iter = front;
}
// Second round: assign random pointers for the copy nodes.
iter = head;
while (iter != NULL) {
if (iter->random != NULL) {
iter->next->random = iter->random->next;
}
iter = iter->next->next;
}
// Third round: restore the original list, and extract the copy list.
iter = head;
Node* pseudoHead = newNode(0);
Node* copy = pseudoHead;
while (iter != NULL) {
front = iter->next->next;
// extract the copy
copy->next = iter->next;
// restore the original list
iter->next = front;
copy = copy->next;
iter = front;
}
return pseudoHead->next;
}
};