mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 07:44:07 +00:00
day 14: 138-copy-list-with-random-pointer
This commit is contained in:
parent
3af727af85
commit
26a6ac63b0
30
138-copy-list-with-random-pointer/driver.cpp
Normal file
30
138-copy-list-with-random-pointer/driver.cpp
Normal file
@ -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;
|
||||||
|
}
|
||||||
71
138-copy-list-with-random-pointer/soln.cpp
Normal file
71
138-copy-list-with-random-pointer/soln.cpp
Normal file
@ -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<pair<int, Node *>> nodemap;
|
||||||
|
vector<pair<int, int>> randindexmap;
|
||||||
|
Node *current = head;
|
||||||
|
for (int i = 0; current != nullptr; i++)
|
||||||
|
{
|
||||||
|
nodemap.push_back(pair<int, Node *>{i, current});
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto it : nodemap)
|
||||||
|
{
|
||||||
|
if (it.second->random == nullptr)
|
||||||
|
{
|
||||||
|
randindexmap.push_back(pair<int, int>{it.first, -1});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (auto itr : nodemap)
|
||||||
|
{
|
||||||
|
if (it.second->random == itr.second)
|
||||||
|
{
|
||||||
|
randindexmap.push_back(pair<int, int>{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<int, Node *>{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;
|
||||||
|
}
|
||||||
24
138-copy-list-with-random-pointer/soln.hpp
Normal file
24
138-copy-list-with-random-pointer/soln.hpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
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);
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user