-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy List with Random Pointer.cpp
63 lines (62 loc) · 1.79 KB
/
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
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if(head==NULL)
{
return NULL;
}
RandomListNode* pNode=head;
RandomListNode* CopyHead=new RandomListNode(pNode->label);
RandomListNode* newNode=CopyHead;
while(pNode!=NULL)//这个就是先把所有结点都生成好,新结点原始结点的next
{
RandomListNode* pNext=pNode->next;
pNode->next=newNode;
newNode->next=pNext;
pNode=pNext;
if(pNode!=NULL)
{
RandomListNode* p=new RandomListNode(pNode->label);
newNode=p;
newNode->next=NULL;
newNode->random=NULL;
}
}
pNode=head;
newNode=CopyHead;
while(pNode!=NULL&&newNode!=NULL)//开始连接随机指针。
{
if(pNode->random!=NULL)//如果有随机指针
{
newNode->random=pNode->random->next;
}
pNode=newNode->next;
if(pNode!=NULL)
{
newNode=pNode->next;
}
}
//接下来的工作就是把这个两个链表拆分开
pNode=head;
newNode=CopyHead;
while(pNode!=NULL&&newNode!=NULL)
{
pNode->next=newNode->next;
pNode=pNode->next;
if(pNode!=NULL)
{
newNode->next=pNode->next;
newNode=newNode->next;
}
}
return CopyHead;
}
};