【原创】leetCodeOj --- Copy List with Random Pointer 解题报告
题目地址:
https://oj.leetcode.com/problems/copy-list-with-random-pointer/
题目内容:
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) {}
* };
*/
方法:
难点在于random指针需要指向一个复制过程中还不存在的结点。解决办法就简单了,先复制完单链表,再处理每个结点的random指针,这样复制random指针时其指向的结点就已经存在了。
为了高效完成这个工作,我们需要建立一个映射,以旧地址为键,新地址为值,方便复制random指针。
全部代码:
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (!head)
return NULL;
unordered_map<int,int> dict;
RandomListNode *tmpHead = head;
RandomListNode *resHead = NULL;
RandomListNode *now = NULL;
RandomListNode *nxt = NULL;
resHead = (RandomListNode *)malloc(sizeof(RandomListNode));
dict[(int)head] = (int)resHead;
resHead->label = tmpHead->label;
resHead->next = NULL;
resHead->random = NULL;
now = resHead;
tmpHead = tmpHead->next;
while (tmpHead)
{
nxt = (RandomListNode *)malloc(sizeof(RandomListNode));
nxt->label = tmpHead->label;
nxt->next = NULL;
nxt->random = NULL;
dict[(int)tmpHead] = (int)nxt;
now->next = nxt;
now = nxt;
tmpHead = tmpHead->next;
}
tmpHead = head;
while (tmpHead)
{
now = (RandomListNode *)dict[(int)tmpHead];
if (tmpHead->random != NULL)
now->random = (RandomListNode *)dict[(int)(tmpHead->random)]; tmpHead = tmpHead->next;
}
return resHead;
}
};
【原创】leetCodeOj --- Copy List with Random Pointer 解题报告的更多相关文章
- [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
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】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...
- [Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode138:Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
随机推荐
- POJ 1159 - Palindrome 优化空间LCS
将原串和其逆序串的最长公共子序列求出来为M..那么2*n-M就是所需要加的最少字符..因为求出的M就是指的原串中"潜伏"的最长回文.. 问题转化为求LCS..但是n最大到5000. ...
- PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程
PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程 - beike - ITeye技术网站 PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程 博客 ...
- Unity3D游戏开发之开发游戏带来的问题
昨日曾就某投资人把移动团队失败原因之中的一个归于选择Unity引擎进行了一番评论,工具本身无罪,但怎样理解工具.正确使用Unity引擎确实须要讨论,在选择Unity之前你也许须要了解下这个引擎实际开发 ...
- 黑马程序员:Java基础总结----类加载器
黑马程序员:Java基础总结 类加载器 ASP.Net+Android+IO开发 . .Net培训 .期待与您交流! 类加载器 Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个 ...
- GMM高斯混合模型学习笔记(EM算法求解)
提出混合模型主要是为了能更好地近似一些较复杂的样本分布,通过不断添加component个数,能够随意地逼近不论什么连续的概率分布.所以我们觉得不论什么样本分布都能够用混合模型来建模.由于高斯函数具有一 ...
- cocos2d-x 类大全及其概要
CCNode 节点类是Cocos2D-x中的主要类,继承自CCObject. 任何需要画在屏幕上的对象都是节点类.最常用的节点类包括场景类(CCScene).布景层类(CCLayer).人物精灵类(C ...
- C语言中字符串如何转换为二进制、八进制、十进制、十六进制
在C语言某个程序当中需要把文本16进制转换成对应的16进制数,比如字符串"0x1a"转换成10进制的26,可以用以下函数来实现 相关函数: atof, atoi, atol, st ...
- poj3974(manacher)
传送门:Palindrome 题意:给定一个字符串,求最长回文子串. 分析:manach裸题,核心理解mx>i?p[i]=min(p[2*id-i],mx-i):1. #pragma comme ...
- Android数据库高手秘籍(一)——SQLite命令
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/38461239 要想熟练地操作不论什么一个数据库.最最主要的要求就是要懂SQL语言, ...
- 公布windows的"Universal Apps" Unity3D游戏
转载请注明出处:http://blog.csdn.net/u010019717 更全的内容请看我的游戏蛮牛地址:http://www.unitymanual.com/space-uid-18602.h ...