-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode138_copy_list_with_random_pointer.cpp
175 lines (144 loc) · 3.9 KB
/
leetcode138_copy_list_with_random_pointer.cpp
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
/**
* @file leetcode138_copy_list_with_random_pointer.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/06/03 13:08
* @brief https://leetcode.com/problems/copy-list-with-random-pointer
*
**/
#include <iostream>
#include <vector>
#include <map>
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
class Solution {
public:
Node* copyRandomList(Node* head) {
std::vector<Node*> nodeVec;
if (head == nullptr) {
return nullptr;
}
Node* p = head;
std::map<Node*, int> nodeMap;
int index = 0;
while (p) {
nodeMap.insert({p, index});
p = p->next;
++index;
}
Node* newHead = nullptr;
Node* cur = nullptr;
p = head;
std::vector<Node*> newNodeVec;
std::vector<int> nextVec;
while (p) {
Node* node = new Node(p->val);
if (newHead == nullptr) {
newHead = node;
cur = node;
} else {
cur->next = node;
cur = cur->next;
}
newNodeVec.emplace_back(node);
if (p->random) {
auto iter = nodeMap.find(p->random);
if (iter != nodeMap.end()) {
nextVec.push_back(iter->second);
} else {
nextVec.push_back(-1);
}
} else {
nextVec.push_back(-1);
}
p = p->next;
}
size_t size = nextVec.size();
for (size_t i = 0; i < size; ++i) {
if (nextVec[i] >= 0) {
newNodeVec[i]->random = newNodeVec[nextVec[i]];
}
}
return newHead;
}
};
struct NodeInfo {
int value;
int random;
};
Node* createListFromVec(std::vector<NodeInfo> nums) {
Node* head = nullptr;
Node* cur = nullptr;
size_t size = nums.size();
if (size <= 0) {
return nullptr;
}
std::vector<Node*> nodeVec;
for (auto x: nums) {
Node* node = new Node(x.value);
if (head == nullptr) {
head = node;
cur = head;
} else {
cur->next = node;
cur = cur->next;
}
nodeVec.push_back(node);
}
for (size_t i = 0; i < size; ++i) {
if (nums[i].random >= 0) {
nodeVec[i]->random = nodeVec[nums[i].random];
}
}
return head;
}
void printListWithRandomPointer(Node* node) {
Node* p = node;
while (p) {
std::cout << p->val << " -> ";
if (p->random) {
std::cout << p->random->val;
} else {
std::cout << "null";
}
std::cout << std::endl;
p = p->next;
}
}
int main() {
std::string s;
while (1) {
std::cout << "Number of elements in the list (Ctrl-C to exit): ";
int n;
if (!(std::cin >> n)) {
return 0;
}
std::cout << n << " (number, pointer) pairs for the list:" << std::endl;
std::vector<NodeInfo> vec;
for (int i = 0; i < n; ++i) {
int ele;
int pointer;
std::cin >> ele >> pointer;
NodeInfo nodeInfo;
nodeInfo.value = ele;
nodeInfo.random = pointer;
vec.emplace_back(nodeInfo);
}
Node* l = createListFromVec(vec);
printListWithRandomPointer(l);
std::cout << "================" << std::endl;
Solution solution;
auto ret = solution.copyRandomList(l);
printListWithRandomPointer(ret);
}
return 0;
}