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表)的更多相关文章

  1. Copy List with Random Pointer leetcode java

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  2. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

  3. 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 ...

  4. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  5. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  6. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  7. LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

  8. 单链表(带random指针)深拷贝(Copy List with Random Pointer)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

  9. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

随机推荐

  1. 在phpnow中配置phpunit

    前面都好了之后,在 D:\phpnow\php-5.2.14-Win32\PEAR 之外的地方执行 phpinfo 都会出现以下错误 Warning: require_once(File/Iterat ...

  2. 关于maven source1.5报错

    是因为maven 默认是1.5编译的 <build>//加上这个配置,把编译给改掉试试 <pluginManagement> <plugins> <plugi ...

  3. SAP CRM点了附件的超链接后报错的处理方式

    SAP CRM系统里,点击了附件的这些超链接后,如果是文本文件,会在浏览器里打开.如果是其他类型的文件,会弹出下载对话框. 然而最近我工作时遇到一个问题,点击超链接后,总是弹出Logon failed ...

  4. 迅为4412开发平台Zigbee模块在物联网智能家居中的应用

      物联网智能家居的发展物联网随着互联网的发展,可以通过互联网实现物和物的互联,就有了物联网的概念.传统家居电器 有了物联网之后,在家居电器范围中,就是通过物联网技术将家中的各种设备连接到一起,家居中 ...

  5. 用list去初始化numpy的array数组 numpy的array和python中自带的list之间相互转化

    http://blog.csdn.net/baiyu9821179/article/details/53365476 a=([3.234,34,3.777,6.33]) a为python的list类型 ...

  6. gcc 变量类型大小 练习 远离 cygwin64 需要带dll

    /* testmini.c -- very simple test program for the miniLZO library */ #include <stdio.h> #inclu ...

  7. 操作系统复习——如何查看一个进程的详细信息,如何追踪一个进程的执行过程 ,如何在 Linux 系统下查看 CPU、内存、磁盘、IO、网卡情况?epoll和select区别?

    1. 如何查看一个进程的详细信息,如何追踪一个进程的执行过程 通过pstree命令(根据pid)进行查询进程内部当前运行了多少线程:# pstree -p 19135(进程号) 使用top命令查看(可 ...

  8. 利用system-config-kickstart实现半自动化安装

    老司机开车了… 上车请坐稳… centos7系统 首先确认已经安装了system-config-kickstart包,如果没有安装就yum install system-config-kickstar ...

  9. 页面布局排版-block,inline,float,relative,absolute等

    1.span和div的区别 div是块元素(block),span是行内元素(inline): span什么事也不会做,它存在的目的在与为开发者给它所围绕的元素指定样式.div类似,不过它引入了行分隔 ...

  10. htmlpurifier的使用

    什么是htmlpurifier?? HTML Purifier是一个可以用来移除所有恶意代码(XSS),而且还能确保你的页面遵循W3C的标准规范的PHP类库. 在php里解决XSS最简单的方法是使用h ...