Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f482663

Browse files
committed
Add clone list with random pointers
1 parent ab95450 commit f482663

File tree

3 files changed

+141
-2
lines changed

3 files changed

+141
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Princeton Algorithms [Part 1](https://www.coursera.org/learn/algorithms-part1/)
8888
* Sort linked list
8989
* Simple method [C++](problems/linked-list/SortLinkedListMethod1.cpp)
9090
* Using merge sort [C++](problems/linked-list/SortLinkedListMethod2.cpp)
91+
* Clone list with random pointers [C++](problems/linked-list/CloneListWithRandomPointer.cpp)
9192

9293
## Stack
9394
* Dynamic Stack [Java](problems/stack/DynamicStack.java)
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Techie Delight https://www.techiedelight.com/clone-a-linked-list-with-random-pointers/
2+
// Leetcode https://leetcode.com/explore/interview/card/top-interview-questions-hard/117/linked-list/841/
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <unordered_map>
7+
8+
class LinkedList
9+
{
10+
private:
11+
class Node
12+
{
13+
public:
14+
int key;
15+
Node* next;
16+
Node* random;
17+
18+
Node(int k) : key(k), next(nullptr) { }
19+
Node() : key(0), next(nullptr) { }
20+
};
21+
22+
Node* head;
23+
24+
void populateRandom()
25+
{
26+
if (head == nullptr) {
27+
return;
28+
}
29+
30+
Node* current = head;
31+
while (current != nullptr) {
32+
if (current->next != nullptr) {
33+
current->random = current->next->next;
34+
}
35+
36+
current = current->next;
37+
}
38+
}
39+
40+
public:
41+
LinkedList() : head(nullptr) { }
42+
43+
~LinkedList()
44+
{
45+
Node* current = head;
46+
while (current != nullptr) {
47+
Node* temp = current;
48+
current = current->next;
49+
delete temp;
50+
}
51+
head = nullptr;
52+
}
53+
54+
void push(int key)
55+
{
56+
Node* node = new Node(key);
57+
if (head != nullptr) {
58+
node->next = head;
59+
}
60+
head = node;
61+
}
62+
63+
void createList(std::vector<int>& keys)
64+
{
65+
for (auto key : keys) {
66+
push(key);
67+
}
68+
populateRandom();
69+
}
70+
71+
void print()
72+
{
73+
if (head == nullptr) {
74+
std::cout << "null\n";
75+
return;
76+
}
77+
78+
Node* current = head;
79+
while (current != nullptr) {
80+
if (current->random != nullptr) {
81+
std::cout << current->key << "(" << current->random->key << ") ";
82+
} else {
83+
std::cout << current->key << "(null) ";
84+
}
85+
current = current->next;
86+
if (current != nullptr) {
87+
std::cout << "-> ";
88+
}
89+
}
90+
std::cout << "\n";
91+
}
92+
93+
Node* cloneList(Node* head, std::unordered_map<Node*, Node*>& map)
94+
{
95+
if (head == nullptr) {
96+
return nullptr;
97+
}
98+
99+
map[head] = new Node(head->key);
100+
map[head]->next = cloneList(head->next, map);
101+
102+
return map[head];
103+
}
104+
105+
void updateRandomPointer(Node* head, std::unordered_map<Node*, Node*>& map)
106+
{
107+
if (map[head] == nullptr) {
108+
return;
109+
}
110+
map[head]->random = map[head->random];
111+
updateRandomPointer(head->next, map);
112+
}
113+
114+
LinkedList clone()
115+
{
116+
LinkedList list;
117+
if (head == nullptr) {
118+
return list;
119+
}
120+
121+
std::unordered_map<Node*, Node*> map;
122+
list.head = cloneList(head, map);
123+
updateRandomPointer(head, map);
124+
125+
return list;
126+
}
127+
};
128+
129+
int main()
130+
{
131+
std::vector<int> keys {6, 3, 4, 8, 2, 9, 10, 5, 1};
132+
LinkedList list;
133+
list.createList(keys);
134+
list.print();
135+
std::cout << "Cloned list : \n";
136+
LinkedList cList = list.clone();
137+
cList.print();
138+
139+
return 0;
140+
}

problems/linked-list/SortLinkedListMethod2.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ class LinkedList
120120
std::cout << "\n";
121121
}
122122

123-
124-
125123
void mergesort()
126124
{
127125
mergesort(&head);

0 commit comments

Comments
 (0)