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. C# 移动控件

    最近要做车牌识别的,不同地区收费标准不一,所以想做个可以移动控件来给客户选择停车场收费条件的.   首先因为要自动排序控件选FlowLayoutPanel做容器,加若干Panel和FlowLayout ...

  2. python实现: protobuf解释器

    之前项目为了自动化,所以写一个protobuf的解释器,用来生成项目所需的格式. 当然现在通过以下链接的指导,跳过手工分析,直接生成代码了. https://developers.google.com ...

  3. Fire Air(华科校赛 网络赛)

    题目 原题链接:https://www.nowcoder.com/acm/contest/106/L 在100000 * 10000的空地上,有n个时间点,每个时间点会在(xi,yi)上种一棵树. 定 ...

  4. Mapping (RESOURCE) not found :和BeanFactory not initialized or already closed - call 'refresh' before access记录

    1.Mapping (RESOURCE) not found :cn/sxx/model/Supplier.hbm.xml : origin(cn/sxx/model/Supplier.hbm.xml ...

  5. springboot @test 使用

    @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class Springtest { ...

  6. strong&weak

    copy:建立一个索引计数为1的对象,然后释放旧对象 对NSString对NSString 它指出,在赋值时使用传入值的一份拷贝.拷贝工作由copy方法执行,此属性只对那些实行了NSCopying协议 ...

  7. JavaScript递归简单实现个对象深拷贝

    JavaScript中对象的深拷贝来说一直都算比较恶心 毕竟没有什么api能直接全拷贝了 得自己便利写  最近在项目中需要深拷贝 自己简单封了个方法 话不多说 直接上码 <!DOCTYPE ht ...

  8. NOIp2018 提高组初赛试题参考答案

  9. InnoDB INFORMATION_SCHEMA System Tables

    InnoDB INFORMATION_SCHEMA System Tables 可以使用InnoDB INFORMATION_SCHEMA系统表提取有关InnoDB管理的schema对象的元数据. 此 ...

  10. c++_等差素数列

    标题:等差素数列 2,3,5,7,11,13,....是素数序列.类似:7,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列.上边的数列公差为30,长度为6. 2004年 ...