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 ...
随机推荐
- JS错误捕获
try/catch/finally错误捕获 try { //一旦try中出现错误,直接跳到执行catch的内容,执行完catch的内容,代码继续执行 throw new Error('错误'); // ...
- 面向对象php基本格式、构造、析构、访问修饰符
<?php//面向对象//1.类//由众多的对象抽象出来的 //2.对象//一切皆对象//由类实例化出来的 //基本形式class 类名{ 成员变量 成员方法 } $对象名 =new 类名 $对 ...
- Leetcode: Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- CPU cache
cache是一种小而快的缓冲器,用在CPU和main memory之间进行数据读写. 在processor访问主memory时,首先检查cache中是不是有一份copy,如果cache hit,则直接 ...
- PhoneGap中navigator.notification.confirm的用法详解
navigator.notification.confirm('您确定要退出程序吗?', showConfirm, '退出程序', '确定,取消'); function showConfirm(but ...
- MS CRM商机产品等Mapping
商机明细:报价明细 SELECT EntityMapId FROM EntityMapBase WHERE TargetEntityName='quotedetail' AND So ...
- mysql复制表结构及检查表、存储过程是否存在
mysql命令行复制表结构的方法: 1.只复制表结构到新表 CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2 或者 CREATE TABLE 新表 LIKE 旧表 ...
- Java堆栈的应用2----------中缀表达式转为后缀表达式的计算Java实现
1.堆栈-Stack 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...
- [转] 停止支持的老版本ubuntu源列表-old-releases
我使用的是ubuntu 9.10,在网上找了大半个月之后,今天终于找到了可用的源地址.感谢这位cgjcgs仁兄. 点击阅读原文 ubuntu的普通版本支持的时间都有限,过了支持的时间,更新源都会被停用 ...
- js 多少天以后的时间
/** * 多少天以后的时间 * @param date 时间 * @param num 多少天 * @param type 类型 年 月 天(默认天) */ exports.afterDate = ...