码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Copy List with Random Pointer

时间:2015-03-19 07:44:36      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

Two methods for doing this problem:

1. With extra memory, using hashtable:

I made a mistake for mapping[runner->random] = mapping[runner]->random. Then it will erase original one and not correctly copied.

 1 /**
 2  * Definition for singly-linked list with a random pointer.
 3  * struct RandomListNode {
 4  *     int label;
 5  *     RandomListNode *next, *random;
 6  *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     RandomListNode *copyRandomList(RandomListNode *head) {
12         if (!head) return NULL;
13         RandomListNode *result = new RandomListNode(head->label);
14         unordered_map<RandomListNode *, RandomListNode *> mapping;
15         mapping[head] = result;
16         RandomListNode *runner = head;
17         while (runner->next) {
18             if (mapping.find(runner->next) == mapping.end()) {
19                 RandomListNode *newNode = new RandomListNode(runner->next->label);
20                 mapping[runner->next] = newNode;
21                 mapping[runner]->next = newNode;
22             }
23             runner = runner->next;
24         }
25         runner = head;
26         while (runner) {
27             mapping[runner]->random = mapping[runner->random];
28             runner = runner->next;
29         }
30         return result;
31     }
32 };

 

2. Merge two list into one and then split them:

A stupid mistake:

when last split two list, I use

while (head->next)

It will cause problem. Because when your head go to the end, the head is NULL, then head->next is an illegal argument.

 1 /**
 2  * Definition for singly-linked list with a random pointer.
 3  * struct RandomListNode {
 4  *     int label;
 5  *     RandomListNode *next, *random;
 6  *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     RandomListNode *copyRandomList(RandomListNode *head) {
12         if (!head) return NULL;
13         RandomListNode *runner = head;
14         RandomListNode *result = new RandomListNode(0);
15         while (runner) {
16             RandomListNode *newNode = new RandomListNode(runner->label);
17             newNode->next = runner->next;
18             runner->next = newNode;
19             runner = runner->next->next;
20         }
21         runner = head;
22         while (runner) {
23             if (runner->random != NULL) {
24                 runner->next->random = runner->random->next;
25             }
26             runner = runner->next->next;
27         }
28         runner = result;
29         while (head) {
30             runner->next = head->next;
31             head->next = head->next->next;
32             head = head->next;
33             runner = runner->next;
34         }
35         return result->next;
36     }
37 };

 

LeetCode – Refresh – Copy List with Random Pointer

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4349357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!