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 ...
随机推荐
- Java maven安装GDAL
1. 使用编译好的安装jdal http://www.gisinternals.com/release.phpgdal-111-1800-x64-core.msi下载地址:http://downloa ...
- mvc 数据验证金钱格式decimal格式验证
mvc 数据验证金钱格式decimal格式验证 首先看下代码 /// <summary> /// 产品单价 /// </summary> [Display(Name = &qu ...
- [Android Tips] 5. INSTALL_PARSE_FAILED_MANIFEST_MALFORMED on Android-2.1
最近在 http://testin.cn 上的多款 android 2.1 设备上出现安装失败的问题 INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 问题分析 貌似 a ...
- 决策树Decision Tree 及实现
Decision Tree 及实现 标签: 决策树熵信息增益分类有监督 2014-03-17 12:12 15010人阅读 评论(41) 收藏 举报 分类: Data Mining(25) Pyt ...
- Matlab的XTickLabel中数值带下标
%axis为'x'或'y',分别表示更改x或y刻度 %ticks是字符cell function settick(axis,ticks) n=length(ticks); tkx=get(gca,'X ...
- java-mvc
定义 一种开发模式 Model-View-Controller Model 模型层 实体类.DAO(模型层实现数据库访问和业务逻辑) Controller 控制层 Servler.Filter(控制层 ...
- Console API 与命令行
一.Console API 当打开 firebug (也包括 Chrome 等浏览器的自带调试工具),window 下面会注册一个叫做 console 的对象,它提供多种方法向控制台输出信息,供开发人 ...
- aaaaaaaaaaaaaaa
<?xml version="1.0" encoding="utf-8"?><document> <!-- 签名,由平台生成 -- ...
- linux下配置redis
安装redis 1.下载文件 wget http://download.redis.io/releases/redis-2.8.12.tar.gz 2.解压文件 tar zxvf redis-2.8 ...
- 关于SQL Server 中连接查询Join的几种常见用法
现有A.B .C 三个表,A中的ID=B中的ID,B中的ID=C中的ID:也就是:A.ID=B.ID,B.ID=C.ID; 一. Inner Join 把两个表链接一起查 Select * from ...