-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0138-Copy_List_With_Random_Pointer.cpp
More file actions
233 lines (207 loc) · 5.34 KB
/
0138-Copy_List_With_Random_Pointer.cpp
File metadata and controls
233 lines (207 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************************
* 0138-Copy_List_With_Random_Pointer.cpp
* Billy.Ljm
* 05 September 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/copy-list-with-random-pointer/
*
* A linked list of length n is given such that each node contains an additional
* random pointer, which could point to any node in the list, or null.
*
* Construct a deep copy of the list. The deep copy should consist of exactly n
* brand new nodes, where each new node has its value set to the value of its
* corresponding original node. Both the next and random pointer of the new
* nodes should point to new nodes in the copied list such that the pointers in
* the original list and copied list represent the same list state. None of the
* pointers in the new list should point to nodes in the original list.
*
* ===========
* My Approach
* ===========
* We'll copy the normal linked list first, then loop again to add the random
* pointers. To make it more time efficient, we can save the linked list nodes
* in a map for faster lookup of its indices when adding the random pointers.
*
* This has a time complexity of O(n), and a space complexity of O(n), where n
* is the length of the linked list.
******************************************************************************/
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Definition for a Node.
*/
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
/**
* Creates linked list from a vector of consecutive nodes, where the nodes
* are specified as {value, index of node of random pointer}
*/
Node* createList(vector<vector<int>> nodes) {
// edge case
if (nodes.size() == 0) return nullptr;
// create nodes
vector<Node*> linkedlist;
for (int i = 0; i < nodes.size(); i++) {
linkedlist.push_back(new Node(nodes[i][0]));
}
// add links
for (int i = 0; i < nodes.size(); i++) {
// normal link
if (i + 1 < nodes.size()) {
linkedlist[i]->next = linkedlist[i + 1];
}
else {
linkedlist[i]->next = nullptr;
}
// random pointer
if (nodes[i][1] >= 0) {
linkedlist[i]->random = linkedlist[nodes[i][1]];
}
else {
linkedlist[i]->random = nullptr;
}
}
return linkedlist[0];
}
/**
* Translates the linked list into a 2D vector for printing. The vector will be
* consecutive links represented as {value, index of node of random pointer}
*/
vector<vector<int>> printList(Node* head) {
vector<vector<int>> out;
Node* crawler;
unordered_map<Node*, int> idx; // map of node to index
// edge case
if (head == nullptr) return vector<vector<int>>();
// fill in map
crawler = head;
while (crawler != nullptr) {
idx[crawler] = idx.size();
crawler = crawler->next;
}
// fill in output vector
crawler = head;
while (crawler != nullptr) {
if (crawler->random != nullptr) {
out.push_back({ crawler->val, idx[crawler->random] });
}
else {
out.push_back({ crawler->val, -1 });
}
crawler = crawler->next;
}
return out;
}
/**
* Deallocate linked list
*/
void deleteList(Node* head) {
// edge case
if (head == nullptr) return;
Node* tmp;
while (head != nullptr) {
tmp = head->next;
delete head;
head = tmp;
}
}
/**
* Solution
*/
class Solution {
public:
Node* copyRandomList(Node* head) {
Node* crawler;
unordered_map<Node*, int> idx; // map of node to index
vector<Node*> copied; // copied nodes
// edge case;
if (head == nullptr) return nullptr;
// first traversal
crawler = head;
while (crawler != nullptr) {
idx[crawler] = idx.size(); // fill in map
copied.push_back(new Node(crawler->val)); // copy nodes
crawler = crawler->next;
}
// second traversal
crawler = head;
while (crawler != nullptr) {
// copy regular links
if (crawler->next != nullptr) {
copied[idx[crawler]]->next = copied[idx[crawler->next]];
}
else {
copied[idx[crawler]]->next = nullptr;
}
// copy random links
if (crawler->random != nullptr) {
copied[idx[crawler]]->random = copied[idx[crawler->random]];
}
else {
copied[idx[crawler]]->random = nullptr;
}
crawler = crawler->next;
}
return copied[0];
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
Node *head, *copied;
vector<vector<int>> nodes;
// test case 1
nodes = { {7,-1},{13,0},{11,4},{10,2},{1,0} };
head = createList(nodes);
copied = sol.copyRandomList(head);
std::cout << "copyRandomList(" << printList(head) << ") = ";
std::cout << printList(copied) << std::endl;
deleteList(head);
deleteList(copied);
// test case 2
nodes = { {1,1},{2,1} };
head = createList(nodes);
copied = sol.copyRandomList(head);
std::cout << "copyRandomList(" << printList(head) << ") = ";
std::cout << printList(copied) << std::endl;
deleteList(head);
deleteList(copied);
// test case 3
nodes = { {3,-1},{3,0},{3,-1} };
head = createList(nodes);
copied = sol.copyRandomList(head);
std::cout << "copyRandomList(" << printList(head) << ") = ";
std::cout << printList(copied) << std::endl;
deleteList(head);
deleteList(copied);
return 0;
}