[Linked List]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) {}
- * };
- */
- class Solution {
- public:
- RandomListNode *copyRandomList(RandomListNode *head) {
- unordered_map<RandomListNode*,RandomListNode*> umap;
- RandomListNode* newHead = NULL;
- RandomListNode* cur = head;
- RandomListNode* node_pre = NULL;
- RandomListNode* node = NULL;
- while(cur){
- node = new RandomListNode(cur->label);
- umap[cur] = node;
- cur == head ? newHead = node :node_pre->next = node;
- node_pre = node;
- cur = cur->next;
- }
- cur = head;
- while(cur){
- umap[cur]->random = cur->random ? umap[cur->random] : NULL;
- cur = cur->next;
- }
- return newHead;
- }
- };
[Linked List]Copy List with Random Pointer的更多相关文章
- 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 ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- 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 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- Leetcode Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- 使用oracle来计算方差及标准差
/* Formatted on 5/24/2012 4:15:58 PM (QP5 v5.149.1003.31008) */ SELECT deptno, ename, ...
- C#避免过长的IF和Switch分支的方法
C#避免过长的IF和Switch分支的方法 1.最蠢形态 //很丑有没有! //这个分支要是一两个还是可以接受的 class Program { static void Main(string[] a ...
- poj 3959 Alignment of Code <vector>“字符串”
Description You are working in a team that writes Incredibly Customizable Programming Codewriter (IC ...
- 层层递进Struts1(六)自定义转换器
Struts提供的类型转换有限,如果我们强行使用没有的类型转换,则会出现错误,以Date类型为例: org.apache.catalina.core.StandardWrapperValve invo ...
- java读取xml(当xml放在包里时)
- jQuery插件教程
http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html 非常不错的jQuery插件教程
- gulp压缩js
1.安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 2.查看nodejs的版本号 3.npm ...
- linux io优化
场景:xml文件解析入库:并备份 问题:磁盘io异常,经常100%busy: linux io优化方法: 1.修改磁盘挂着参数,修改为writeback模式:对于文件读取频繁的可以设置noatime: ...
- http://www.cnblogs.com/Joyes1989/archive/2013/06/28/3161739.html centos 输入法安装切换
昨天装了一个centos 安装输入法的时候 让我有点纠结 全英文的 读不懂
- FATE(费用背包,没懂)
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...