Skip to content

Solution #138 - Daniel/Edited - 14.03.2025 #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Goal was to create a deep copy of a linked list with a random pointer along with the next pointer.

Initial approach was to create a vector of the linked list to be able to map indices to each of the nodes.
Then a hashmap to map the Node to its corresponding index.
Then a vector to store the new nodes.
Then a hashmap to map the indices of the new nodes to their indices.
Finally the random pointer of each node would be mapped to the corresponding index of the prev list, using both hashmaps.

Optimal approach is to use a single hashmap to just
map old node to new node in the first traversal.
Second traversal to find the next and random pointers of the new nodes based on the values of the old node keys.

Interweaved approach creates new nodes and inserts them in the new list.
By traversing the interweaved list, we can assign the next node of the old node's random pointer to the new node's random pointer. One more traversal would be required to separate the two. However, I haven't implemented that approach here.
Comment on lines +1 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution looks good, but Kindly improve the explanation.md file just like the other PR's (Markdown + Step by Step breakdown + Time and Space Complexity explanation)

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Solution {
public:
Node* copyRandomList(Node* head) {
// brute forced using two vectors and two hashmaps.
vector<Node*> v;
Node* temp = head;
unordered_map<int, int> umap;
unordered_map<Node*, int> vmap;
int k = 0;
while(temp != NULL){
v.push_back(temp);
vmap[temp] = k;
k++;
temp = temp->next;
}
for(int i=0; i<v.size(); i++){
umap[i] = (v[i]->random != NULL)?vmap[v[i]->random]:-1;
}
Node* new_head = new Node(0);
Node* new_tail = new_head;
vector<Node*> new_v;
for(int i=0; i<v.size(); i++){
Node* x = new Node(v[i]->val);
new_tail -> next = x;
new_tail = new_tail -> next;
new_v.push_back(x);
}
Node* tail = new_head -> next;
for(int i=0; i<v.size(); i++){
tail->random = (umap[i] != -1)?new_v[umap[i]]: NULL;
tail = tail->next;
}
return new_head->next;

// optimized using single hashmap
Node* temp = head;
unordered_map<Node*, Node*> umap;
while(temp != NULL){
Node* x = new Node(temp->val);
umap[temp] = x;
temp = temp->next;
}
Node* temp2 = head;
while(temp2 != NULL){
umap[temp2]->next = (temp2->next)?umap[temp2->next]:NULL;
umap[temp2]->random = (temp2->random)?umap[temp2->random]:NULL;
temp2 = temp2->next;
}
return umap[head];

}
};