-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyRandomList.h
75 lines (61 loc) · 1.75 KB
/
copyRandomList.h
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
//
// Created by yindong on 19-6-3.
//
#ifndef SRC_COPYRANDOMLIST_H
#define SRC_COPYRANDOMLIST_H
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node() {}
Node(int _val, Node* _next, Node* _random) {
val = _val;
next = _next;
random = _random;
}
};
#include<map>
using namespace std;
class Solution {
public:
map<Node*, Node*> m;
Node* copyRandomList(Node* head) {
if(head == NULL){
return NULL;
}
Node *cur, *new_cur, *new_last, *new_head;
new_head = new Node(head->val, NULL, NULL);
// new_head->val = head->val;
// cout << "insert: " << head << ' ' << new_head << endl;
m.insert({head, new_head});
cur = head->next;
new_last = new_head;
// 遍历链表,进行深拷贝
while(cur != NULL){
new_cur = new Node(cur->val, NULL, NULL);
// new_cur->val = cur->val;
new_last->next = new_cur;
// cout << "insert: " << cur << ' ' << new_cur << endl;
m.insert({cur, new_cur});
cur = cur->next;
new_last = new_cur;
}
// 再次遍历新旧链表,找到每个随机指针对应的新节点,将新链表的随机指针指向新节点
Node *ptr = head, *new_ptr = new_head;
while(ptr != NULL){
if(ptr->random != NULL){
cout << ptr->random << ' ' << m[ptr->random] << endl;
new_ptr->random = m[ptr->random];
}
else{
new_ptr->random = NULL;
}
ptr= ptr->next;
new_ptr = new_ptr->next;
}
return new_head;
}
};
#endif //SRC_COPYRANDOMLIST_H