-
Notifications
You must be signed in to change notification settings - Fork 0
/
138. 复制带随机指针的链表.cc
66 lines (57 loc) · 1.39 KB
/
138. 复制带随机指针的链表.cc
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
/*
// 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) {
//处理random指针
unordered_map<int, Node*> srcIdMap; //$id->Node 当前的node id映射到random指针上
unordered_map<Node*, int> srcNodeMap; //Node->$id 当前的node*映射到node id上
unordered_map<int, Node*> dstIdMap;
Node *header = nullptr;
if(head == nullptr) return header;
int id = 1;
header = new Node(head->val);
Node *n = header;
srcIdMap[id] = head->random;
srcNodeMap[head] = id;
dstIdMap[id] = n;
++id;
/* 遍历原链表 */
Node *p = head->next;
while(p)
{
Node *t = new Node(p->val);
n->next = t;
n = n->next;
srcIdMap[id] = p->random;
srcNodeMap[p] = id;
dstIdMap[id] = t;
++id;
p = p->next;
}
//处理random指针
p = header;
int i = 1;
while(p)
{
int obj = srcNodeMap[srcIdMap[i]];
p->random = dstIdMap[obj];
++i;
p = p->next;
}
assert(i == id);
return header;
}
};