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.

这题用map做其实比较简单,但是一开始没想明白越想越乱,最后看了下别人的实现,思路还是很清晰的,代码如下所示:

 /**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/ class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
unordered_map<RandomListNode *, RandomListNode *> m;
RandomListNode helper1(), * p1 = &helper1, helper2(), *p2 = &helper2;
p1->next = head;
while(p1->next){
p1 = p1->next;
RandomListNode * tmpNode = new RandomListNode(p1->label);
m[p1] = tmpNode;
p2 = p2->next = tmpNode;
}
p1 = &helper1;
while(p1->next){
p1 = p1->next;
if(p1->random){
m[p1]->random = m[p1->random];
}
}
return helper2.next;
}
};

LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)的更多相关文章

  1. [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

    public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...

  2. [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  3. [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  4. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现

    题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...

  6. [LeetCode OJ] Copy List with Random Pointer 扩大

    职务地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意:对一个有回路的链表的深复制 解题:这道题我AC了之后才发 ...

  7. [Leetcode Week17]Copy List with Random Pointer

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

  8. leetcode 138. Copy List with Random Pointer复杂链表的复制

    python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...

  9. Java for LeetCode 138 Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

随机推荐

  1. 20145310第一周JAVA实验报告

    20145310第一周JAVA实验报告 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 使用JDK和IDE编译.运行简单 ...

  2. 20145211《网络渗透》MS12-004漏洞渗透

    20145211<网络渗透>MS12-004漏洞渗透 一 实验原理 初步掌握平台matesploit的使用 有了初步完成渗透操作的思路 在这里我选择对的不是老师推荐的MS11_050,而是 ...

  3. python-运算、分支、深浅拷贝

      算术表达式: + - * / 除法Python3中是默认向浮点数靠拢 //取整运算   结果的最小整数靠拢  向下 5 // 2 = 2(向下取整) %取余运算 5 % 2 = 1 **幂值运算 ...

  4. 终极之shell-zsh全解析

    什么是Zsh Zsh是一款强大的虚拟终端,既是一个系统的虚拟终端,也可以作为一个脚本语言的交互解析器. Zsh的一些特性 兼容bash,原来使用bash的兄弟切换过来毫无压力. 强大的历史纪录功能,在 ...

  5. 跟着我一起学习大数据——Hadoop

    hadoop配置文件:http://archive.cloudera.com/cdh5/cdh/5/hadoop-2.6.0-cdh5.9.0/ 一:Hadoop简介 总结下起源于Nutch项目,社区 ...

  6. LeetCode——Find All Duplicates in an Array

    Question Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice an ...

  7. codeforce 589B枚举

    2017-08-25 12:00:53 writer:pprp 很简单的枚举,但是我调试了很长时间,出现各种各样的问题 /* theme:cf 589B writer:pprp declare:枚举 ...

  8. linux主机之间无密钥ssh访问

    ssh-keygen -t rsa ssh-copy-id -i /root/.ssh/id-rsa.pub root@10.0.0.109 # 实现和109互通 vim /etc/hosts DNS ...

  9. wireshark抓包分析

    TCP协议首部: 分析第一个包: 源地址:我自己电脑的IP,就不放上来了 Destination: 222.199.191.33 目的地址 TCP:表明是个TCP协议 Length:66 表明包的长度 ...

  10. webstorm自动换行

    1.文件 — — 设置 2. 编辑器 — — 编辑器 — — 在编辑窗口使用软换行(勾选)