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 ...
随机推荐
- Spring MVC 复习笔记03
1. @RequestMapping 1). url映射 定义controller方法对应的url,进行处理器映射使用. 2). 窄化请求映射 3). 限制http请求方法 出于安全性考虑,对htt ...
- linux上使用wget下载文件
首次安装的centos操作系统是没有安装wget的,所以首先需要先安装wget,然后才能使用wget下载文件. 1.第一步,保证centos能正常连网.使用命令 :yum -y install wg ...
- php与js 编码解码交互
javascript: var fontcolorEncode=encodeURIComponent(fontcolor.value); //编码 php: $fontcolordecode= u ...
- OpenCV KNN 之 使用方法
http://blog.csdn.net/WL2002200/article/details/43149229 OpenCV 中KNN构造函数如下. C++: CvKNearest::CvKNeare ...
- hadoop HA + kerberos HA集群搭建问题和测试总结
1. 常见问题 (1)hostname设置问题.vi /etc/sysconfig/network (2)集群/etc/hosts没有统一. (3)yarn slave需要单独启动../sbin/y ...
- 20145303 《Java程序设计》第六周学习总结
20145303 <Java程序设计>第六周学习总结 教材学习内容总结 第十章:输入/输出 (InputStream与OutputStream) 1.Java将输入/输出抽象化为串流,数据 ...
- KALI LINUX系统初始化配置
1.Kali Linux安装VirtualBox增强功能 VirtualBox增强功能介绍:物理机与虚拟机之间的文件共享.物理机与虚拟机之间的剪切板共享.虚拟机的direct3D支持,这样虚拟机窗口就 ...
- Ubuntu16.04 + Digits + caffee
reference: csdn 必要依赖包安装 sudo apt-get install build-essential sudo apt-get install --no-install-recom ...
- Dive into Spring framework -- 了解基本原理(二)--设计模式-part2
Template模式 Template模式顾名思义是提供了一种模板,也就是针对某种业务提供了模范框架.这个在spring中是属于核心模式的,因为其ApplicationContext抽象类就是模板模式 ...
- linux文件的一些特殊权限
一些特殊权限 虽然我们通常看到一个八进制的权限掩码用三位数字来表示,但是从技术层面 上来讲,用四位数字来表示它更确切些. 为什么呢?因为,除了读取,写入,和执 行权限之外,还有其它的,较少用到的权限设 ...