题目

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. DELL R730安装ESXI虚拟化

    dell安装esxi需要dell官方提供的镜像文件地址:http://www.dell.com/support/article/us/en/04/SLN290857/dell%E5%AE%9A%E5% ...

  2. pta 编程题16 Saving James Bond - Easy Version

    其它pta数据结构编程题请参见:pta 题目 主要用到了深度优先搜索. #include <iostream> using namespace std; struct Vertex { i ...

  3. c++树的表示方法

    c++树的节点的表示方法: typedef struct Node *Tree; struct Node { int data; Node *left; Node *right; int flag; ...

  4. Hybris Enterprise Commerce Platform 服务层的设计与实现

    Hybris Enterprise Commerce Platform这个系列之前已经由我的同事,SAP成都研究院Hybris开发团队的同事张健(Zhang Jonathan)发布过两篇文章了.这里J ...

  5. IOS 截屏(保存到相册中)

    @interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...

  6. windows下安装python的包管理工具pip,scikit-learn

    打开https://pip.pypa.io/en/latest/installing.html#python-os-support 下载pip-get.py 进入python,执行pip-get.py ...

  7. NodeJS--exports和module.exports

    继续迁移印象笔记中记录相关笔记,其实工作中遇到的很多问题当时解决了,后期就忘记了,多记录还是很有用的,好记性不如烂笔头嘛,以后要养成好习惯. NodeJS中 require 用来加载代码,而 expo ...

  8. Python 正则表达式 利用括号分组

    如果想把区号从匹配的电话号码中分离,可以添加括号在正则表达式中创建分组,再使用group()方法,从一个分组中获取匹配的文本 正则表达式字符串中,第一个括号是第一组,第二个括号是第二组.向group( ...

  9. day09-函数讲解

    1.如何定义一个函数 s = '华为加油a' def s_len(): i = 0 for k in s: i += 1 print(i) s_len() 这个函数的功能就是输出字符串的长度.但是他只 ...

  10. Flow Problem HDU - 3549

    Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...