LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
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.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
原来的链表的结构如下图所示:
注意一点,random的指针可以指向后面的结点,也可以指向前面的结点。
这里提供两种解法:
方法1:用hash表存储结点信息,时间O(2n),空间O(n)
第一次遍历原链表,并构建random为null的新链表,于此同时在hash表中存储原结点和新结点的地址信息,原结点地址为key,新结点地址为value,即map<原结点地址,新结点地址>
第二次遍历原链表,对于random不为空的结点,可以根据random的值,在hash表中找到random指向的节点对应的新结点的地址,再以此给新结点random赋值即可。
方法2:先改变原链表的结构,在恢复,时间O(2n),空间O(1)
这个方法需要3次遍历,
第一次,构建新链表的结点,random为null,并且用原来链表节点的next指向对应的新结点,新结点的next指向原链表的下一个结点,如图所示:
第二次,给新节点的random赋值,
p = head;
while(p!=null){
if(p.random != null){
p.next.random = p.random.next;
}
p = p.next.next;
}
第三次,恢复原链表和新链表的链表结构。
head2 = head.next;
p = head;
while(p!=null){
p2 = p.next;
p.next = p2.next;
if (p2.next != null) p2.next = p2.next.next;
p = p.next;
}
方法2的完整代码:
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head == null)return null;
RandomListNode head2,p2,p = head;
while(p!=null){
RandomListNode n = new RandomListNode(p.label);
n.next = p.next;
p.next = n;
p = n.next;
}
p = head;
while(p!=null){
if(p.random != null){
p.next.random = p.random.next;
}
p = p.next.next;
} head2 = head.next;
p = head;
while(p!=null){
p2 = p.next;
p.next = p2.next;
if (p2.next != null) p2.next = p2.next.next;
p = p.next;
}
return head2;
}
}
LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)的更多相关文章
- [LeetCode] 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]138. Copy List with Random Pointer复制带有随机指针的链表
public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...
- leetcode题解: Remove Duplicates from Sorted List(已排序单链表去重)
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- 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 单链表相关题目汇总
leetcode-19-Remove Nth From End of List—移除链表中倒数第n个元素 leetcode-21-Merge Two Sorted Lists—两个已排序链表归并 ...
- [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 ...
随机推荐
- linux下重启apache
基本的操作方法: 本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令: 推荐/usr/local/apache2/bin/apachec ...
- Linux 下Nginx编译安装
Untitled .note-content {font-family: 'Helvetica Neue', Arial, 'Hiragino Sans GB', STHeiti, 'Microsof ...
- Java文件写入,换行
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOExce ...
- NPOI 导出Excel
NPOIExcel npoiexcel = new NPOIExcel(); string filename = DateTime.Now.ToString("yyyyMMddHHmmssf ...
- centos ADSL 拨号上网设置
下面主要介绍一下,在CentOS命令行环境下如何配置 ADSL 联网: 1.确保安装了网卡并能正常运行,使用命令查看一下网卡状态: [root@localhost simon]# /sbin/ifco ...
- 从yield关键字看IEnumerable和Collection的区别
C#的yield关键字由来以久,如果我没有记错的话,应该是在C# 2.0中被引入的.相信大家此关键字的用法已经了然于胸,很多人也了解yield背后的“延迟赋值”机制.但是即使你知道这个机制,你也很容易 ...
- VirtualBox Guest Additions 在CentOS中无法安装的解决方法
安装时出现一步错误查看log为(log文件是 /var/log/vboxadd-install.log): /tmp/vbox.0/Makefile.include.header:94: *** Er ...
- 万恶的jar包冲突
搭了个spring+struts2+mybatis的项目架子, 好久不用myEclipse和tomcat了,生疏了好多. 建议还是去百度一些框架整合的博客,直接使用博客里面给的jar包列表里的jar包 ...
- XML Schema and XMLspy notes
Introduction An xml documents consists of elements, attributes and text. There are two structures in ...
- PHP如何快速读取大文件
在PHP中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数,简简单单的几行代码就能 很漂亮的完成我们所需要的功能.但当所操作的文件是一个比较大的 ...