From cac38b51160b866b2e00ee5b6f5e6cb148123eb3 Mon Sep 17 00:00:00 2001 From: danzang100 <12danysamuel@gmail.com> Date: Fri, 14 Mar 2025 15:49:12 +0530 Subject: [PATCH] Solution #138 - Daniel/Edited - 14.03.2025 --- .../Explanation-Copy List Random.md | 14 +++++ .../Solution-Copy List Random.cpp | 52 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Linked Lists/#138-Copy List with Random Pointer-Medium/Explanation-Copy List Random.md create mode 100644 Linked Lists/#138-Copy List with Random Pointer-Medium/Solution-Copy List Random.cpp diff --git a/Linked Lists/#138-Copy List with Random Pointer-Medium/Explanation-Copy List Random.md b/Linked Lists/#138-Copy List with Random Pointer-Medium/Explanation-Copy List Random.md new file mode 100644 index 0000000..6374905 --- /dev/null +++ b/Linked Lists/#138-Copy List with Random Pointer-Medium/Explanation-Copy List Random.md @@ -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. diff --git a/Linked Lists/#138-Copy List with Random Pointer-Medium/Solution-Copy List Random.cpp b/Linked Lists/#138-Copy List with Random Pointer-Medium/Solution-Copy List Random.cpp new file mode 100644 index 0000000..d98c60a --- /dev/null +++ b/Linked Lists/#138-Copy List with Random Pointer-Medium/Solution-Copy List Random.cpp @@ -0,0 +1,52 @@ +class Solution { + public: + Node* copyRandomList(Node* head) { + // brute forced using two vectors and two hashmaps. + vector v; + Node* temp = head; + unordered_map umap; + unordered_map vmap; + int k = 0; + while(temp != NULL){ + v.push_back(temp); + vmap[temp] = k; + k++; + temp = temp->next; + } + for(int i=0; irandom != NULL)?vmap[v[i]->random]:-1; + } + Node* new_head = new Node(0); + Node* new_tail = new_head; + vector new_v; + for(int i=0; ival); + new_tail -> next = x; + new_tail = new_tail -> next; + new_v.push_back(x); + } + Node* tail = new_head -> next; + for(int i=0; irandom = (umap[i] != -1)?new_v[umap[i]]: NULL; + tail = tail->next; + } + return new_head->next; + + // optimized using single hashmap + Node* temp = head; + unordered_map 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]; + + } + }; \ No newline at end of file