博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Copy List with Random Pointer
阅读量:5835 次
发布时间:2019-06-18

本文共 3364 字,大约阅读时间需要 11 分钟。

Well, since we need to make a deep copy of the list and nodes in the list have a random pointer that may point to any node in the list (or NULL), we need to maintain a mapping from each node in the origianl list to the node in the copy list. If the copy of a node already exists, just use that copy without copying it again; otherwise, create a new copy and add it to the mapping.

The following code is pretty straight-forward.

1 class Solution { 2 public: 3     RandomListNode *copyRandomList(RandomListNode *head) { 4         if (!head) return NULL; 5         mp[head] = new RandomListNode(head -> label); 6         RandomListNode* run = head; 7         RandomListNode* temp = mp[head]; 8         while (run) { 9             /* Process the next pointer */10             if (run -> next) {11                 if (mp.find(run -> next) == mp.end())12                     mp[run -> next] = new RandomListNode(run -> next -> label);13                 temp -> next = mp[run -> next];14             }15             /* Process the random pointer */16             if (run -> random) {17                 if (mp.find(run -> random) == mp.end())18                     mp[run -> random] =  new RandomListNode(run -> random -> label);19                 temp -> random = mp[run -> random];20             }21             run = run -> next;22             temp = temp -> next;23         }24         return mp[head];25     }26 private:27     unordered_map
mp;28 };

This code also has a very concise and easy-to-understand recursive solution as follows.

1 class Solution { 2 public: 3     RandomListNode *copyRandomList(RandomListNode *head) { 4         if (!head) return NULL; 5         if (mp.find(head) != mp.end()) 6             return mp[head]; 7         RandomListNode* node = new RandomListNode(head -> label); 8         mp[head] = node; 9         node -> next = copyRandomList(head -> next);10         node -> random = copyRandomList(head -> random);11         return mp[head];12     }13 private:14     unordered_map
mp;15 };

has a clever solution using O(1) extra space, which is rewritten below in C++.

1 class Solution { 2 public: 3     RandomListNode *copyRandomList(RandomListNode *head) { 4         if (!head) return NULL; 5         RandomListNode* run = head; 6         /* Insert the copy of each node after it. */ 7         while (run) { 8             RandomListNode* copy = new RandomListNode(run -> label); 9             copy -> next = run -> next;10             run -> next = copy;11             run = run -> next -> next;12         }13         /* Set the random pointer for each copy. */14         run = head;15         while (run) {16             if (run -> random)17                 run -> next -> random = run -> random -> next;18             run = run -> next -> next;19         }20         /* Extract the copy list. */21         RandomListNode* new_head = new RandomListNode(0);22         RandomListNode* new_run;23         run = head;24         new_head -> next = head -> next;25         while (run) {26             new_run = run -> next;27             run -> next = new_run -> next;28             if (run -> next)29                 new_run -> next = new_run -> next -> next;30             run = run -> next;31         }32         return new_head -> next;33     }34 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4645132.html

你可能感兴趣的文章
Linux中rc的含义
查看>>
实现跨交换机VLAN间的通信
查看>>
Java基础之String,StringBuilder,StringBuffer
查看>>
安卓中数据库的搭建与使用
查看>>
AT3908 Two Integers
查看>>
C++ 0X 新特性实例(比较常用的) (转)
查看>>
.NET 设计规范--.NET约定、惯用法与模式-2.框架设计基础
查看>>
win7 64位+Oracle 11g 64位下使用 PL/SQL Developer 的解决办法
查看>>
BZOJ1997:[HNOI2010]PLANAR——题解
查看>>
HTML5新手入门指南
查看>>
opennebula 开发记录
查看>>
sql 内联,左联,右联,全联
查看>>
C++关于字符串的处理
查看>>
Breaking parallel loops in .NET C# using the Stop method z
查看>>
修改故障转移群集心跳时间
查看>>
[轉]redis;mongodb;memcache三者的性能比較
查看>>
微软职位内部推荐-Sr DEV
查看>>
让你的WPF程序在Win7下呈现Win8风格主题
查看>>
802.11 学习笔记
查看>>
Leetcode-Database-176-Second Highest Salary-Easy(转)
查看>>