题目

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.

代码

/**
* 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 ) return head;
std::map<RandomListNode *, RandomListNode *> oriNext;
RandomListNode *pOri = head;
RandomListNode dummy(-);
RandomListNode *pCopy = &dummy;
// first round visit
while (pOri)
{
// store the original Node's next pointer
oriNext[pOri] = pOri->next;
// create copy Node
pOri->next = new RandomListNode(pOri->label);
pCopy->next = pOri->next;
pCopy->next->random = pOri;
pCopy = pCopy->next;
// move to the next Node
pOri = oriNext[pOri];
}
// second round visit
pCopy = dummy.next;
while (pCopy)
{
pCopy->random = pCopy->random->random ? pCopy->random->random->next : NULL;
pCopy = pCopy->next;
}
// third round recover original list
for (std::map<RandomListNode *, RandomListNode *>::iterator i = oriNext.begin(); i != oriNext.end(); ++i)
{
i->first->next = i->second;
}
return dummy.next;
}
};

Tips

典型的链表深拷贝,之前用python做的时候用了O(1)空间技巧(http://www.cnblogs.com/xbf9xbf/p/4216655.html)。

这次尝试了用hashmap的O(n)空间的技巧,原因是感觉hashmap可能更通用一些。

思路主要参考了下面这个日志:http://www.geeksforgeeks.org/a-linked-list-with-next-and-arbit-pointer/

需要注意的是:如果原来的链表节点random为空,就不要再往下寻找next;需要加一个保护判断

===============================================================

第二次过这道题,hashmap的大体思路画画图还记得,代码调试了几次才AC。

(1)不要忘记恢复原来的链表

(2)在拷贝random的时候,不要忘记copy = copy->next这条语句

/**
* 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) return NULL;
map<RandomListNode*, RandomListNode*> ori_next;
RandomListNode* p = head;
RandomListNode dummpy(-);
RandomListNode* copy = &dummpy;
while (p)
{
copy->next = new RandomListNode(p->label);
copy->next->random = p;
ori_next[p] = p->next;
p = p->next;
copy->next->random->next = copy->next;
copy = copy->next;
}
copy = dummpy.next;
while (copy)
{
copy->random = copy->random->random ? copy->random->random->next : NULL;
copy = copy->next;
}
for ( map<RandomListNode*, RandomListNode*>::iterator i=ori_next.begin(); i!=ori_next.end(); ++i )
{
i->first->next = ori_next[i->first];
}
return dummpy.next;
}
};

【Copy List with Random Pointer】cpp的更多相关文章

  1. leetcode 【 Copy List with Random Pointer 】 python 实现

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  2. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  3. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

  4. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

  5. Copy List with Random Pointer leetcode java

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  6. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  7. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  8. 【leetcode】Copy List with Random Pointer (hard)

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. 【原创】leetCodeOj --- Copy List with Random Pointer 解题报告

    题目地址: https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目内容: A linked list is given s ...

随机推荐

  1. FRM-92050错误

    使用IE8在打开EBS Form界面时,窗口提示信息“Internet Explorer 已对此页面进行了修改,以帮助阻止跨站脚本.单击此处,获取详细信息...”或者R12 IE8中出"FR ...

  2. 配置海康IPC或大华IPC通过路由器公网访问

    设备:路由器DLink-DIR-600M,海康IPC:DS-2CD864FWD-E 海康默认端口为8000,HTTP访问为80,RTSP访问端口为554. 配置分成两步,分别为配置IPC相关网络参数和 ...

  3. 不该被忽视的CoreJava细节(四)

    令人纳闷的数组初始化细节 这个细节问题我很久以前就想深入研究一下,但是一直没有能够抽出时间,借这系列文章的东风,尽量解决掉这个"心头病". 下面以一维int数组为例,对数组初始化方 ...

  4. Yii2 widgets [mztest/yii2-widget-file-upload]

    Enjoy it. A widget for uploading files to your server. Github , Packagist Screenshots

  5. 关于调用百度地图api在自己写的网页中实现和解决在https中正常显示

    百度地图开发者:http://lbsyun.baidu.com/index.php?title=jspopular 我们打开百度地图开发者网站,注册一个百度账号,然后打开控制台,开始创建应用:(如果你 ...

  6. SAP成都研究院CEC团队三巨头之一:M君的文章预告

    国人总倾向于把特点或者作用类似的人或物放在一起比较并做出排名,于是就有了许多"某某某三巨头"的称谓. 最举世闻名的莫过于二战三巨头:丘吉尔,罗斯福和斯大林. 还有陪伴咱八零后童年时 ...

  7. 如何解析比特币中的交易原始数据rawData

    交易数据结构 有关交易的详细信息可以查看比特币的wiki网站:Transaction TxBinaryMap: 原始图片地址 交易的结构表格(Transaction): 示例数据 以一个正式网络的一笔 ...

  8. 设置和重置ssh key

    查看本地是否有已经生成好的ssh key $ cat ~/.ssh/id_rsa.pub 若有,先删除: $ cd ~ $ rm -rf .ssh 重新生成ssh key ssh-keygen -t ...

  9. python 补缺收集

    [http://www.cnblogs.com/happyframework/p/3255962.html] 1. 高效code 与 不常用的函数用法: #带索引的遍历 , )): print(ind ...

  10. 2018.5.18 AndroidStudio创建项目出错

    Android Studio 出现 Gradle's dependency cache may be corrupt 错误分析 Error:Failed to open zip file. Gradl ...