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 ...
随机推荐
- sqlit3事务
事务定义了一组SQL命令的边界,这组命令或者作为一个整体被全部执行,或者都不执行.事务的典型实例是转帐. 事务的范围 事务由3个命令控制:BEGIN.COMMIT和ROLLBACK.BEGIN开始一个 ...
- java 面试题整理
java面试题 1.接口和抽象类的区别 抽象类 接口 抽象类中可以有默认方法 在java8之前,不能有默认方法 extends implements 抽象类中可以有构造器 接口中不能有构造器 抽象类中 ...
- 《少年先疯队》第九次团队作业:Beta冲刺第三天
3.1 今日完成任务情况 姚玉婷:酒店系统中剩余功能的完善 马丽莎:酒店系统中管理员功能的测试 张 琼:酒店系统中会员功能的测试 孙苗坤:酒店系统中其余管理功能的测试文档的编写 3.2 成员贡献时 ...
- AndroidStudio连不上天天模拟器
问题:天天模拟器经常无法被Android Studio读取出来: 解决方法:手动连接它的端口: 方法一:找到Android\SDK\platform-tools目录,在当前目录下打开命令行窗口(shi ...
- 01Ping程序的设计
1.Ping程序设计具体设计任务 1.1 实验目的 PING程序是我们使用的比较多的用于测试网络连通性的程序.PING程序基于ICMP,使用ICMP的回送请求和回送应答来工作.由计算机网络课程知道,I ...
- 24. TABLES
24. TABLES TABLES表提供有关数据库中表的信息. TABLES表有以下列: TABLE_CATALOG :表所属目录的名称.该值始终为def. TABLE_SCHEMA :表所属sche ...
- ubuntu修改网卡名称ensX为eth0
1.sudo nano /etc/default/grub 找到GRUB_CMDLINE_LINUX="" 改为GRUB_CMDLINE_LINUX="net.ifnam ...
- 第一次:从今开始玩Linux,Ubuntu16.04
首先声明,我的文章不配图,就靠文字描述,然后自己体会,摸着石头体验吧! 从今天开始玩Linux,Ubuntu16.04据说是比较稳定的,界面友好,类似与Windows界面,也有Linux的命令终端,用 ...
- memcached内存分配
Memcached默认情况下采用了名为Slab Allocator的机制分配.管理内存,最大单个存储对象大小为1M. page:分配给slab的最小内存空间,默认为1M,可以在启动时通过-l参数修改 ...
- 条款35:考虑virtual函数以外的其他选择(Consider alternative to virtual functions)
NOTE: 1.virtual 函数的替代方案包括NVI手法及Strategy设计模式的多种形式.NVI手法自身是一个特殊形式的Template Method设计模式. 2.将机能从成员函数移到外部函 ...