题目

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. VC使用编译时间作为版本号

    常用方法分两步:1. 得到编译时间:2. 设置基准时间,以编译时间距基准时间的总天数的2倍作为版本号,适当情况还可加上初值: 其中第一步实现有两种方法: 1. 直接使用系统宏:CString OcxT ...

  2. cocos2d-x 学习资料汇总

    cocos2d-x配置问题 - 我要飞的更高 - 博客频道 - CSDN.NET Cocos2d-x win7 + vs2010 配置图文详解(亲测) - 子龙山人 - 博客园 WINDONWS7+V ...

  3. uvm_regex——DPI在UVM中的实现(三)

    UVM的正则表达是在uvm_regex.cc 和uvm_regex.svh 中实现的,uvm_regex.svh实现UVM的正则表达式的源代码如下: `ifndef UVM_REGEX_NO_DPI ...

  4. NYOJ-198-数数

    原题地址 数数 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 我们平时数数都是喜欢从左向右数的,但是我们的小白同学最近听说德国人数数和我们有些不同,他们正好和我们相 ...

  5. IOS UIImageView的帧动画

    ● UIImageView可以让一系列的图片在特定的时间内按顺序显示 ● 相关属性解析: ● animationImages:要显示的图片(一个装着UIImage的NSArray) ● animati ...

  6. IOS 响应者链条 and UIGestureRecognizer 手势识别器)

    一次完整的触摸事件的传递响应的过程 UIAppliction --> UIWiondw -->递归找到最适合处理事件的控件 控件调用touches方法-->判断是否实现touches ...

  7. 项目开发中dev、test和prod是什么意思

    开发环境(dev):开发环境是程序猿们专门用于开发的服务器,配置可以比较随意,为了开发调试方便,一般打开全部错误报告. 测试环境(test):一般是克隆一份生产环境的配置,一个程序在测试环境工作不正常 ...

  8. Java 集合框架_上

    集合框架被设计成要满足以下几个目标. 该框架必须是高性能的.基本集合(动态数组,链表,树,哈希表)的实现也必须是高效的. 该框架允许不同类型的集合,以类似的方式工作,具有高度的互操作性. 对一个集合的 ...

  9. Hive 之元数据库的三种模式

    Hive 介绍 http://www.cnblogs.com/sharpxiajun/archive/2013/06/02/3114180.html Hive的数据类型和数据模型 http://www ...

  10. python_46_输出

    name='Qi Zhiguang' name2='ZhangMeng' print("Hi!"+name)#用加号,后边must be str print('Hi!',name) ...