138. Copy List with Random Pointer (Graph, Map; DFS)
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.
struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if(!head) return NULL;
map<RandomListNode *, RandomListNode *> flag;
RandomListNode *root = copyNode(head, flag);
return root;
}
RandomListNode * copyNode(RandomListNode *source, map<RandomListNode *, RandomListNode *> &flag)
{
if(flag.find(source)!=flag.end())
{
return flag[source];
}
RandomListNode *target = new RandomListNode (source->label);
flag[source]=target;
if(source->next)
target->next = copyNode (source->next,flag);
if(source->random)
target->random = copyNode (source->random,flag);
return target;
}
};
138. Copy List with Random Pointer (Graph, Map; DFS)的更多相关文章
- 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 ...
- 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 ...
- [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 138. Copy List with Random Pointer ----- java
A linked list is given such that each node contains an additional random pointer which could point t ...
- 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 poi ...
- 138. Copy List with Random Pointer (not do it by myself)
A linked list is given such that each node contains an additional random pointer which could point t ...
- 138 Copy List with Random Pointer 复制带随机指针的链表
给出一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点.返回一个深拷贝的链表. 详见:https://leetcode.com/problems/copy-list- ...
随机推荐
- Winform开发之ADO.NET对象Connection、Command、DataReader、DataAdapter、DataSet和DataTable简介
ADO.NET技术主要包括Connection.Command.DataReader.DataAdapter.DataSet和DataTable等6个对象,下面对这6个对象进行简单的介绍:(1)Con ...
- ubuntu16.04x64环境下 tar方式 安装mysql-5.7.21 试水过程记录
前几天读研时候上铺的同学和我说到了一个问题,就是他们单位的redhat服务器给MySQL服务的数据库文件所在的磁盘空间不够了,对于这个问题我也是没有想过的,在受朋友之托下考虑自己做下复现,由于同学所在 ...
- 【css样式生成 & 图片合并压缩工具】Sprite,你值得拥有
好久好久没有更新博客了,越来越懒...话说懒也有懒的好处,懒的时候你可能会想着用些神马方法来帮你偷懒.没错,下面就给大家介绍个博主前不久开发的[css样式生成 & 图片合并压缩工具]Spirt ...
- sublime text2下配置c++
今天安装了sublime text2,真是编辑神器,不再用notepad了. 笔记本上没有c++运行环境,用编辑器既轻巧,又方便,VS太臃肿了. 要在sublime text2 下运行c++程序,需要 ...
- Cockpit 容器&&kubernetes 管理可视化工具
安装 在k8s 的master 上 yum install -y cockpit cockpit-ws cockpit-kubernetes cockpit-bridge cockpit-dashbo ...
- js中setAttribute 的兼容性
js中setAttribute 的兼容性class和className兼容方法: object.setAttribute("class","content") ...
- RK3288 红外遥控器增加自定义按键
转载请注明出处:https://www.cnblogs.com/lialong1st/p/10071557.html CPU:RK3288 系统:Android 5.1 1.在 dts 中增加红外遥控 ...
- 【linux】Linux系统信息查看命令大全
系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...
- GCC参数详解 二
1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...
- 黄聪:VS2010编辑C#未启动,打开设计视图时报"未将对象引用设置到对象的实例"
通常情况下,若是你将用户控件写好了放入窗体中,若是有不合理的代码,则会弹出错误提示框,不让你放.若是你之前只是随便加了一个用户控件,并且没有什么问题,但后来你又把控件改坏掉了,那么你打开就会报错(在窗 ...