From 26a6ac63b0b4389b2b5305f96870acf8cc9b2a92 Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Wed, 11 Oct 2023 11:42:48 -0700 Subject: [PATCH] day 14: 138-copy-list-with-random-pointer --- 138-copy-list-with-random-pointer/driver.cpp | 30 +++++++++ 138-copy-list-with-random-pointer/soln.cpp | 71 ++++++++++++++++++++ 138-copy-list-with-random-pointer/soln.hpp | 24 +++++++ 3 files changed, 125 insertions(+) create mode 100644 138-copy-list-with-random-pointer/driver.cpp create mode 100644 138-copy-list-with-random-pointer/soln.cpp create mode 100644 138-copy-list-with-random-pointer/soln.hpp diff --git a/138-copy-list-with-random-pointer/driver.cpp b/138-copy-list-with-random-pointer/driver.cpp new file mode 100644 index 0000000..5b3f0c0 --- /dev/null +++ b/138-copy-list-with-random-pointer/driver.cpp @@ -0,0 +1,30 @@ +#include "soln.hpp" + +int main() +{ + Node n1(7); + Node n2(13); + Node n3(11); + Node n4(10); + Node n5(1); + n1.next = &n2; + n2.next = &n3; + n2.random = &n1; + n3.next = &n4; + n3.random = &n5; + n4.next = &n5; + n4.random = &n3; + n5.random = &n1; + + Solution soln; + Node *n_1 = soln.copyRandomList(&n1); + + Node *h1 = &n1, *h_1 = n_1; + while (h1 != nullptr && h_1 != nullptr) + { + cout << h1->val << ", " << ((h1->random != nullptr) ? h1->random->val : -9999) << endl; + cout << h_1->val << ", " << ((h_1->random != nullptr) ? h_1->random->val : -9999) << endl; + h1 = h1->next, h_1 = h_1->next; + } + return 0; +} \ No newline at end of file diff --git a/138-copy-list-with-random-pointer/soln.cpp b/138-copy-list-with-random-pointer/soln.cpp new file mode 100644 index 0000000..7e941c5 --- /dev/null +++ b/138-copy-list-with-random-pointer/soln.cpp @@ -0,0 +1,71 @@ +#include "soln.hpp" + +void Solution::insertNode(Node *head, Node *newnode) +{ + newnode->next = head->next; + head->next = newnode; +} +Node *Solution::copyRandomList(Node *head) +{ + if (head == nullptr) + { + return nullptr; + } + vector> nodemap; + vector> randindexmap; + Node *current = head; + for (int i = 0; current != nullptr; i++) + { + nodemap.push_back(pair{i, current}); + current = current->next; + } + + for (auto it : nodemap) + { + if (it.second->random == nullptr) + { + randindexmap.push_back(pair{it.first, -1}); + continue; + } + for (auto itr : nodemap) + { + if (it.second->random == itr.second) + { + randindexmap.push_back(pair{it.first, itr.first}); + break; + } + } + } + + Node *new_head_ptr = new Node(nodemap[0].second->val); + current = new_head_ptr; + for (int i = 1; i < nodemap.size(); i++) + { + Node *new_node = new Node(nodemap[i].second->val); + insertNode(current, new_node); + current = current->next; + } + + nodemap.clear(); + current = new_head_ptr; + for (int i = 0; current != nullptr; i++) + { + nodemap.push_back(pair{i, current}); + current = current->next; + } + + for (int i = 0; i < nodemap.size(); i++) + { + + if (randindexmap[i].second == -1) + { + nodemap[i].second->random = nullptr; + } + else + { + nodemap[i].second->random = nodemap[randindexmap[i].second].second; + } + } + + return new_head_ptr; +} \ No newline at end of file diff --git a/138-copy-list-with-random-pointer/soln.hpp b/138-copy-list-with-random-pointer/soln.hpp new file mode 100644 index 0000000..d204174 --- /dev/null +++ b/138-copy-list-with-random-pointer/soln.hpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +class Node +{ +public: + int val; + Node *next; + Node *random; + + Node(int _val) + { + val = _val; + next = NULL; + random = NULL; + } +}; + +class Solution +{ +public: + void insertNode(Node *head, Node *newnode); + Node *copyRandomList(Node *head); +}; \ No newline at end of file