-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26复杂链表的复制.cpp
56 lines (48 loc) · 1.39 KB
/
26复杂链表的复制.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
//26
//首先理清 暴力解法的拷贝思路与步骤
/*
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
*/
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead)
{
if(pHead == nullptr)
return nullptr;
//1、复制结点,并串起来
RandomListNode *pCurrNode = pHead;
while(pCurrNode)
{
RandomListNode *pNewNode = new RandomListNode(pCurrNode->label);
pNewNode->next = pCurrNode->next;
pCurrNode->next = pNewNode;
pCurrNode = pNewNode->next;
}
//2、“移位”,完成*random的指向
pCurrNode = pHead;
while(pCurrNode)
{
if(pCurrNode->random)
pCurrNode->next->random = pCurrNode->random->next;
pCurrNode = pCurrNode->next->next;
}
//3、拆成 原始链表与拷贝链表
pCurrNode = pHead;
RandomListNode *pNewHead = pHead->next, *pNewNode = pNewHead;
while(pNewNode->next)//链尾处,画图分析一下
{
pCurrNode->next = pNewNode->next;
pNewNode->next = pCurrNode->next->next;
pCurrNode = pCurrNode->next;
pNewNode = pNewNode->next;
}
pCurrNode->next = nullptr;
return pNewHead;
}
};