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. 获取页面URL两种方式

    以请求http://localhost:8080/doctor/demo?code=1为例 一:用java代码获取 //获取URL中的请求参数.即?后的条件 code=1 String querySt ...

  2. codevs 2600 13号星期几?

    时间限制: 1 s  空间限制: 8000 KB  题目等级 : 黄金 Gold 题目描述 Description 从1900年1月1日(星期一) 开始经过的n年当中,每个月的13号这一天是星期一.星 ...

  3. pocket API学习笔记

    最近安装了pocket离线阅读软件. 为了收藏需要的URL,每次都要打开浏览器.然后按google工具条上的pocket+. 网页多的时候,这个过程就非常缓慢. 根据pocket网站的API介绍,我可 ...

  4. Android系统固件定制方式

    target_product.mkAndroid系统在构建关于某种产品的固件时,一般会根据特定于该产品的具体target_product.mk来配置生成整个Android系统./target_prod ...

  5. droplang - 删除一种 PostgreSQL 过程语言

    SYNOPSIS droplang [ connection-option...] langname [ dbname] droplang [ connection-option...] --list ...

  6. MySQL系列(二)--MySQL存储引擎

    影响数据库性能的因素: 1.硬件环境:CPU.内存.存盘IO.网卡流量等 2.存储引擎的选择 3.数据库参数配置(影响最大) 4.数据库结构设计和SQL语句 MySQL采用插件式存储引擎,可以自行选择 ...

  7. clone对象或数组

    function clone(obj) { var o; if (typeof obj == "object") { if (obj === null) { o = null; } ...

  8. Windows下使用ffmpeg与java实现截取视频缩略图

    [ffmpeg.exe可执行文件获取]: 网上搜索后得到编译好的ffmpeg文件,下载地址:http://download.csdn.net/source/453719 [安装]: 将下载的文件解压, ...

  9. modify django app models.py adn settings.py

    from django.db import models from django.contrib import admin # from personal import models class Us ...

  10. iOS 6 的 Smart App Banners 介绍和使用

    iOS 6 的 Smart App Banners 介绍和使用 Denis 留言: 10 浏览:4890 文章目录[隐藏] 什么是 Smart App Banners 在你的网站添加 Smart Ap ...