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 ...
随机推荐
- CentOS 6.8_x64 Oracle 12C 安装
1.下载地址 (需要注册oracle账号) 点击 2.登录CentOS 做准备工作 groupadd oinstall groupadd dba useradd -g oinstall -g dba ...
- Spring AOP动态切换数据源
现在稍微复杂一点的项目,一个数据库也可能搞不定,可能还涉及分布式事务什么的,不过由于现在我只是做一个接口集成的项目,所以分布式就先不用了,用Spring AOP来达到切换数据源,查询不同的数据库就可以 ...
- BZOJ 2882: 工艺
2882: 工艺 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 599 Solved: 268[Submit][Status][Discuss] D ...
- Werewolf流程分析
werewolf大致流程 首先是房主创建房间,创建成功以后房主开启web socket连接. 其他成员加入房间,加入房间后新成员和老成员的游戏玩家列表都会更新,然后新成员也要开启web socket连 ...
- Vue.JS 对比其他框架
Angular 选择 Vue 而不选择 Angular,有下面几个原因,当然不是对每个人都适合: 在 API 与设计两方面上 Vue.js 都比 Angular 简单得多,因此你可以快速地掌握它的全部 ...
- Github上fork了别人的项目之后如何同步代码
其实很简单,如下: fork了别人代码到自己仓库,然后把自己仓库的代码clone下来 在本地添加远程仓库添加fork的代码库,git remote add xxx url git pull xxx m ...
- BZOJ3172: [Tjoi2013]单词
传送门 做了这么多题怎么还是无法很好的理解AC自动机呢..果然是个制杖 首先题意表述不是很清晰,这些所有的单词组成了那个文章,所以果断建个AC自动机,建的时候给每个点附加一个权值,建树是经过一次权值即 ...
- easyUI 中datagrid 返回列隐藏方法
easyui的datagrid方法返回的列,有的值不需要显示可以使用hidden(属性进行隐藏) columns : [ [{ field : 'bailClass', title : '类别', w ...
- A Quick Introduction to Linux Policy Routing
A Quick Introduction to Linux Policy Routing 29 May 2013 In this post, I’m going to introduce you to ...
- bzoj1045 糖果传递
escription 老师准备了一堆糖果, 恰好n个小朋友可以分到数目一样多的糖果. 老师要n个小朋友去拿糖果, 然后围着圆桌坐好, 第1个小朋友的左边是第n个小朋友, 其他第i个小朋友左边是第i-1 ...