Copy List with Random Pointer [LeetCode]
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.
Solution: Uses map to recorde node mapping between old linked list and new linked list.
RandomListNode *copyRandomList(RandomListNode *head) {
map<RandomListNode *, RandomListNode *> old_to_new;
RandomListNode * current = head;
RandomListNode * new_head = NULL;
RandomListNode * pre = NULL;
while(current != NULL) {
RandomListNode * node = new RandomListNode(current->label);
old_to_new[current] = node;
if(new_head == NULL){
new_head = node;
pre = node;
}else if(pre != NULL){
pre->next = node;
pre = pre->next;
} current = current->next;
} current = head;
RandomListNode * new_current = new_head;
while(current != NULL && new_current != NULL) {
if(current->random != NULL)
new_current->random = old_to_new[current->random];
else
new_current->random = NULL;
current = current->next;
new_current = new_current->next;
}
return new_head;
}
Copy List with Random Pointer [LeetCode]的更多相关文章
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- [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 ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
随机推荐
- .NET中string[]数组和List<string>泛型的相互转换以及Array类的Sort()方法(转)
从string[]转List<string>: " }; List<string> list = new List<string>(str); 从List ...
- cocos2dx 3.x(在Mac平台下利用Eclipse打包安卓apk安装包详细教程)
最近在学习cocos2dx在MAC上如何打包apk,今天先把安装JDK和ANT的过程记来. 首先,打开终端,输入"java -version" 点击回车后,出现如下提示: 我们的M ...
- RDIFramework.NET ━ 9.6 模块(菜单)管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.6 模块(菜单)管理 -Web部分 模块(菜单)管理是整个框架的核心,主要面向系统管理人员与开发人员,对普通用户建议不要授 ...
- openssl 证书操作命令
生成Self Signed证书 # 生成一个key,你的私钥,openssl会提示你输入一个密码,可以输入,也可以不输, # 输入的话,以后每次使用这个key的时候都要输入密码,安全起见,还是应该有一 ...
- HttpClient_001_初步实现项目01的servlet,与项目02的servlet,之间数据访问
HttpClient_001_初步实现项目01的servlet,与项目02的servlet,之间数据访问 代码下载地址: http://download.csdn.net/detail/poiuy19 ...
- [转]网络诊断工具:MTR
MTR是Linux平台上一款非常好用的网络诊断工具,集成了traceroute.ping.nslookup的功能,用于诊断网络状态非常有用.能按要求对路由中所有节点进行批量测试 第一列(Host):I ...
- dotfiles管理
刚刚知道dotfiles这个东西,百度也没发现什么太有价值的讲解,还都是英文,所以自己立志来好好屡屡清楚 1.dotfiles是什么?我自己的理解:linux下(mac下)有各种app,每个人会根据自 ...
- Linux命令学习整理。
http://www.cnblogs.com/suliuer/p/5448747.html 本文主要包括两部分,一是Linux基础命令的总结:二是总结一些常用的命令知识点. 一.基础总结 学习Linu ...
- Masonry+拖动
最近遇到一个问题,用Masonry写的布局: 拖动其中某个view,拖动方法按传统的写成如下形式.如果view中的label更改text值,拖动之后的view就会回到最初被设定的位置. - (void ...
- zabbix通过API创建交换机模板,ifAdminStatus;ifOperStatus;ifInUcastPkts;ifAlias
最终效果: 目的: 通过zabbix的Latest data查看主机就可以看到其监控结果. 监控项: # 管理状态 IF-MIB::ifAdminSt ...