【Copy List with Random Pointer】cpp
题目:
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的更多相关文章
- leetcode 【 Copy List with Random Pointer 】 python 实现
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 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 ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【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 ...
- 【原创】leetCodeOj --- Copy List with Random Pointer 解题报告
题目地址: https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目内容: A linked list is given s ...
随机推荐
- Winform 读取 指定\另一个\其他\任意 配置文件
ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @&qu ...
- Yii2 Working with Relational Data at ActiveDataProvider
Yii2 Working with Relational Data at ActiveDataProvider namespace common\models; use Yii; use yii\ba ...
- oracle 的启动与连接
1. Oracle的启动 oracle的服务如下图所示: 启动oracle有两个重要的服务(如上图标识处): l OracleOraDb11g_home1TNSListener:监听服务,主要用于客户 ...
- COGS 898. [咲 -Saki-] 天才麻将少女什么编
★☆ 输入文件:sakinani.in 输出文件:sakinani.out 简单对比时间限制:1 s 内存限制:256 MB 题目背景 二十一世纪,世界上的麻将竞技人数超过一亿,日本每 ...
- 详情介绍win7:编辑文件夹时提示操作无法完成,因为其中的文件夹或文件已在另一个程序中打开的解决过程
我们在使用电脑中,总会遇到下面这种情况: 那怎么解决呢,现在就开始教程: 在电脑的底下显示各种图标那一行点击右键,再选择“启动任务管理器” 接下来你就可以对你刚刚要操作的文件进行重命名.删除等操作啦! ...
- python_11_guess任性玩
age_of_oldboy=56 count=0 while count<3: guess_age=int(input("guess age:")) if guess_age ...
- yield 生成器的运行机制
yield 生成器的运行机制 当你问生成器要一个数时,生成器会执行,直至出现 yield 语句,生成器把 yield 的参数给你,之后生成器就不会往下继续运行. 当你问他要下一个数时,他会从上次的状态 ...
- CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第二节
原文链接 第二节:第一个内核 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的实验室进行大型并 ...
- Abode Dreamweaver cc 安装与激活
原文链接Abode Dreamweaver CC是Adobe宣布放弃Creative Suite系列产品后推出的新版Creative Cloud产品,功能上修复了CS6中出现的选取代码不精准的问题,最 ...
- 查询Linux下已安装软件的版本
#rpm -qa | grep mysql