leetcode - Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

 

个人思路:

1,先遍历一遍链表,创建出复制链表,并且复制链表的next指针已经连接好,同时用一个map存储原结点和新结点的映射关系

2,同时遍历一遍原链表和复制链表,此时关注random指针,根据原结点random指针指向的结点和map表,便可找到对应的复制结点,使复制的random指针指向该复制结点即可

代码:

 1 #include <stddef.h>
 2 #include <map>
 3 
 4 using namespace std;
 5 
 6 struct RandomListNode
 7 {
 8     int label;
 9     RandomListNode *next, *random;
10     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
11 };
12 
13 class Solution {
14 public:
15     RandomListNode *copyRandomList(RandomListNode *head) {
16         if (!head)
17         {
18             return NULL;
19         }
20 
21         map<RandomListNode*, RandomListNode*> original2copy;        
22         RandomListNode *originalCurrent = head->next;
23         RandomListNode *copyHead = new RandomListNode(head->label);
24         RandomListNode *copyCurrent = copyHead;
25 
26         original2copy[head] = copyHead;
27 
28         while (originalCurrent) //先创建copy结点和连接next指针,并记录配对信息
29         {
30             copyCurrent->next = new RandomListNode(originalCurrent->label);
31             copyCurrent = copyCurrent->next;
32             original2copy[originalCurrent] = copyCurrent;
33             originalCurrent = originalCurrent->next;
34         }
35 
36         originalCurrent = head;
37         copyCurrent = copyHead;
38         while (originalCurrent) //连接random指针
39         {
40             if (originalCurrent->random)
41             {
42                 copyCurrent->random = original2copy[originalCurrent->random];
43             }
44 
45             originalCurrent = originalCurrent->next;
46             copyCurrent = copyCurrent->next;
47         }
48 
49         return copyHead;
50     }
51 };
View Code

 

还有一个更加巧妙的思路,将复制结点放在原结点之后,这样一来,复制结点的random指针指向的结点便是原结点random指针指向结点的next结点,最后将原链表和复制链表拆开即可,这个思路比上面那个省去了O(n)的空间消耗

代码:

 1 #include <stddef.h>
 2 
 3 struct RandomListNode
 4 {
 5     int label;
 6     RandomListNode *next, *random;
 7     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 8 };
 9 
10 class Solution {
11 public:
12     RandomListNode *copyRandomList(RandomListNode *head) {
13         if (!head)
14         {
15             return NULL;
16         }
17 
18         RandomListNode *current = head;
19         RandomListNode *copy = NULL;
20 
21         while (current) //创建复制链表,并将复制链表的结点连到原结点之后
22         {
23             copy = new RandomListNode(current->label);
24             copy->next = current->next;
25             current->next = copy;
26             current = copy->next;
27         }
28 
29         current = head;
30         
31         while (current) //设置复制链表结点的random指针
32         {
33             if (current->random)
34             {
35                 current->next->random = current->random->next;
36             }
37             current = current->next->next;
38         }
39 
40         RandomListNode *copyHead = head->next;
41         current = head;
42 
43         while (current) //将原链表和复制链表拆开
44         {
45             copy = current->next;
46             current->next = copy->next;
47             current = current->next;
48             if (current)
49             {
50                 copy->next = current->next;
51             }
52         }
53 
54         return copyHead;
55     }
56 };
View Code

 

posted on 2014-09-02 09:33  laihaiteng  阅读(122)  评论(0编辑  收藏  举报

导航