-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0138_copyRandomList.cc
89 lines (84 loc) · 1.75 KB
/
Problem_0138_copyRandomList.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <unordered_map>
using namespace std;
class Node
{
public:
int val;
Node* next;
Node* random;
Node(int _val)
{
val = _val;
next = NULL;
random = NULL;
}
};
class Solution
{
public:
// 空间复杂度O(N)
Node* copyRandomList1(Node* head)
{
unordered_map<Node*, Node*> map;
Node* p = head;
while (p != nullptr)
{
Node* node = new Node(p->val);
node->next = p->next;
node->random = p->random;
map.emplace(p, node);
p = p->next;
}
p = map[head];
while (p != nullptr)
{
p->next = map[p->next];
p->random = map[p->random];
p = p->next;
}
return map[head];
}
Node* copyRandomList2(Node* head)
{
if (head == nullptr)
{
return nullptr;
}
Node* cur = head;
Node* next = nullptr;
// 1 -> 2 -> 3 -> null
// 1 -> 1' -> 2 -> 2' -> 3 -> 3'
while (cur != nullptr)
{
next = cur->next;
cur->next = new Node(cur->val);
cur->next->next = next;
cur = next;
}
cur = head;
Node* copy = nullptr;
// 1 1' 2 2' 3 3'
// 依次设置 1' 2' 3' random指针
while (cur != nullptr)
{
next = cur->next->next;
copy = cur->next;
// cur->random->next这一步相当于查hashmap,跟方法一异曲同工
copy->random = cur->random != nullptr ? cur->random->next : nullptr;
cur = next;
}
Node* res = head->next;
cur = head;
// 老 新 混在一起,next方向上,random正确
// next方向上,把新老链表分离
while (cur != nullptr)
{
next = cur->next->next;
copy = cur->next;
cur->next = next;
copy->next = next != nullptr ? next->next : nullptr;
cur = next;
}
return res;
}
};