LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)
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(复制存在随机链接的链表)的更多相关文章
- [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表
public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...
- [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 ...
- [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 ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现
题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...
- [LeetCode OJ] Copy List with Random Pointer 扩大
职务地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意:对一个有回路的链表的深复制 解题:这道题我AC了之后才发 ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- leetcode 138. Copy List with Random Pointer复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
- 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 ...
随机推荐
- Java 为什么要使用反射(通俗易懂的举例)
Java反射最大的好处就是能在运行期间,获得某个类的结构.成员变量,用来实例化. 下列是具体使用场景:假如我们有两个程序员,一个程序员在写程序的时候,需要使用第二个程序员所写的类,但第二个程序员并没完 ...
- Hive练习
一.基础DDL练习 SHOW DATABASES; CREATE DATABASE IF NOT EXISTS db1 COMMENT 'Our database db1'; SHOW DATABAS ...
- python windows打包
接触过的工具有pyinstaller,或者py2exe.感觉pyinstaller更简单易用. 真正将依赖的dll打包称一个安装包还需要借助windows打包工具 Inno Setup 或 NSIS ...
- sublime text3 授权码
适用于 Sublime Text 3 Build3126 64位 官方版 -– BEGIN LICENSE -– Michael Barnes Single User License EA7E-821 ...
- linux下安装sphinx
1.下载sphinx源码包 上面截图的这个网址 复制链接地址 在putty终端使用:wget http://sphinxsearch.com/files/sphinx-2.3.1-beta.t ...
- git gc内存错误的解决方案
Auto packing the repository for optimum performance. You may alsorun "git gc" manually. Se ...
- 【bzoj4423】[AMPPZ2013]Bytehattan(平面图转对偶图+并查集)
题目传送门:bzoj4423 如果是普通的删边判连通性,我们可以很显然的想到把操作离线下来,倒着加边.然而,这题强 制 在 线. 虽然如此,但是题目所给的图是个平面图.那么我们把它转成对偶图试试看? ...
- 【ML数学知识】极大似然估计
它是建立在极大似然原理的基础上的一个统计方法,极大似然原理的直观想法是,一个随机试验如有若干个可能的结果A,B,C,... ,若在一次试验中,结果A出现了,那么可以认为实验条件对A的出现有利,也即出现 ...
- jdk官网历史版本下载&Oracle账号密码
jdk官网下载地址: https://www.oracle.com/technetwork/java/javase/archive-139210.html 账号密码: 1772885836@qq.co ...
- 编译binutil包报错cc: error trying to exec 'cc1obj': execvp: No such file or directory
在http://forums.fedoraforum.org/showthread.php?t=267449中找到的解决方法 $LFS/sources/binutils-2.15.91.0.2/gpr ...