Copy List with Random Pointer (Hash表)
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.
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if(head==NULL) return head;
map<RandomListNode*,int> hash;
map<int,RandomListNode*> copyHash;
RandomListNode* copyHead=new RandomListNode(head->label);
RandomListNode* p=head;
RandomListNode* q=copyHead;
int index=;
hash[p]=index;
copyHash[index]=q;
++index;
p=p->next;
//复制整个链表
while(p!=NULL){
RandomListNode* newNode=new RandomListNode(p->label);
q->next=newNode;
q=q->next;
hash[p]=index;
copyHash[index]=q;
++index;
p=p->next;
}
//最后的NULL节点也要加进去
hash[NULL]=index;
copyHash[index]=NULL;
//复制random指针
int i=;
RandomListNode* r=head;
while (i<hash.size()&&r!=NULL)
{
copyHash[i]->random=copyHash[hash[r->random]];
++i;
r=r->next;
}
return copyHead;
}
};
Copy List with Random Pointer (Hash表)的更多相关文章
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 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 ...
- 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(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- 单链表(带random指针)深拷贝(Copy List with Random Pointer)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
随机推荐
- Javaweb学习笔记8—DBUtils工具包
今天来讲javaweb的第8阶段学习. DBUtils技术,DBUtils是我们操作数据库很常用的功能,虽然后期使用都是它的封装结果,但是也需要掌握. 老规矩,首先先用一张思维导图来展现今天的博客内容 ...
- sql格式化工具
桌面版: SQLInform: http://www.sqlinform.com/download_free_desktop_sw.html 在线格式化: http://www.dpriver.com ...
- learnpythonthehardway EX41 相关
str.count() # str.count()方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. # str.count(sub, start= 0,end=len( ...
- 如何实现Windows宿主系统和虚拟机ubuntu系统文件互相访问
我的宿主操作系统是Windows 10,使用Oracle的Virtual Box安装了Ubuntu. 因为工作需要我经常得在两个系统之间互相拷贝一些数据,下面是具体步骤,可以实现Windows 10和 ...
- C-基础:函数返回局部变量
一般的来说,函数是可以返回局部变量的. 局部变量的作用域只在函数内部,在函数返回后,局部变量的内存已经释放了.因此,如果函数返回的是局部变量的值,不涉及地址,程序不会出错.但是如果返回的是局部变量的地 ...
- Linux下安装Redis5.0.2
1.下载redis 地址 http://download.redis.io/releases/redis-5.0.2.tar.gz 2.解压tar -zxf redis-5.0.2.tar.gz 3. ...
- 01-mysql中的数据类型
mysql中的列数据类型:数值型.字符串类型.日期/时间类型3种 几种列类型描述使用了下述惯例:· M #表示最大显示宽度.最大有效显示宽度是255.· D #适用于浮点和定点类型,表示小数点后面的位 ...
- 简单的自动化使用--使用selenium实现学习通网站的刷慕课程序。注释空格加代码大概200行不到
简单的自动化使用--使用selenium实现学习通网站的刷慕课程序.注释空格加代码大概200行不到 相见恨晚啊 github地址 环境Python3.6 + pycharm + chrom浏览器 + ...
- 第六天,字典Dictionary
字典(Dictionary)在Python中是一种可变的容器模型,它是通过一组键(key)值(value)对组成,这种结构类型通常也被称为映射,或者叫关联数组,也有叫哈希表的.每个key-value之 ...
- <c:foreach> <c:forTokens>
<c:choose>标签与Java switch语句的功能一样,用于在众多选项中做出选择. switch语句中有case,而<c:choose>标签中对应有<c:when ...