138. 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.
/**
* 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) { }
};
============
这个链表节点和普通链表的区别就是,有一个random指针,可以指向链表中的任何元素或者为nullptr
返回产生一个深copy.
思路:
第一遍,对list的每一个节点复制一次放在节点后面,这一次只复制节点的next指针,不复制节点的random指针
第一遍结束后我们的链表节点
a->b->c->d... 会变成a->fake_a->b->fake_b->c->fake_c->d->d->fake_d....
第二遍再对链表进行random指针复制
第三遍堆链表进行拆分,这样的我们可以将链表分离出来一个新的链表.
code如下:
/**
* 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){
RandomListNode *curr = head;
while(curr){
RandomListNode *node = new RandomListNode(curr->label);
node->next = curr->next;
curr->next = node;
curr = node->next;
}///a->f_a-> b->f_b-> ...
curr = head;
while(curr){
if(curr->random){
curr->next->random = curr->random->next;
}
curr = curr->next->next;
} ///partition it into two linktable
RandomListNode dummy(-);
RandomListNode *h = &dummy;
curr = head;
while(curr){
h->next = curr->next;
curr->next = curr->next->next;
h = h->next;
curr = curr->next;
}
h->next = nullptr;
return dummy.next;
}
};
138. Copy List with Random Pointer的更多相关文章
- 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 ...
- [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 ...
- 【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 (Graph, Map; DFS)
A linked list is given such that each node contains an additional random pointer which could point t ...
- 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 ...
- 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 ...
- [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 ...
- 138 Copy List with Random Pointer 复制带随机指针的链表
给出一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点.返回一个深拷贝的链表. 详见:https://leetcode.com/problems/copy-list- ...
随机推荐
- Matlab 矩阵卷积理解(转载)
转载自:http://blog.csdn.net/andrewseu/article/details/51783181 在图像处理的过程中,经常会看到矩阵卷积的概念,比如说用一个模板去和一张图片进行卷 ...
- API中FileReader 接口事件
FileReader 接口包含了一套完整的事件模型,用于捕获读取文件时的状态onabort 中断onerror 出错onloadstart 开始onprogress ...
- php遇见的错误(一)
1.linux 执行脚本时报的错 Call to a member function on a non-object in 是没有实例化对象 解决方法 new类2.在给一个表增加一个新字段或者改 ...
- JSBinding + SharpKit / 常见问题
运行时出现: Return a "System.Xml.XmlIteratorNodeList" to JS failed. Did you forget to export th ...
- mysql学习之-字符集选定,修改。
基础概念:字符(Character)是指人类语言中最小的表义符号.例如'A'.'B'等:编码(Encoding)是指给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符.例如,我们给字符'A ...
- OCR文字识别软件 怎么识别包含非常规符号的文本
ABBYY FineReader 12 是一款OCR图文识别软件,可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的文本,有时文本中可能会包含一些非常规的符号,此时ABBYY ...
- OSPF(Open Shortest Path First开放式最短路径优先 -链路状态路由协议
OSPF分为OSPFv2和OSPFv3两个版本,其中OSPFv2用在IPv4网络,OSPFv3用在IPv6网络 思科OSPF的协议管理距离(AD)是110,华为OSPF的协议管理距离是10 通告网络接 ...
- Dubbo架构设计详解-转
Dubbo架构设计详解 2013-09-03 21:26:59 Yanjun Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解 ...
- 解决outlook不能显示鼠标问题
今天发现打开outlook2010后, 没有鼠标显示. 解决方案: Control Panel -> Mouse Settings ->Pointer Options Uncheck th ...
- 查看当前web服务器的并发连接数
对于web服务器来说,并发连接数是一个比较重要的参数,通过下面的命令就可以直接查看# netstat -nat | grep ":80"| grep EST | wc -l命令解释 ...