Copy List with Random Pointer
https://leetcode.com/problems/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.
解题思路:
啥叫deep copy,啥叫shallow copy?Java里shallow copy的意思是,B拷贝于A,但B和A指向同一个对象。deep copy的意思是,B和A指向不同的对象,但这两个对象完全一样。
具体可以参考 http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java
那么这道题目的难度就在于多了一个random指针。当前节点和next很容易复制,遍历一遍,不断根据原来链表的值去new新节点,并将next指向下一节点即可。问题是,第二遍的时候,如何找到当前节点的random节点?
这里借用的是HashMap这个数据结构,第一遍遍历的时候,建立一个map,key是原链表的节点,value是当前链表的节点。我们知道,实际上map里存的是相应的地址。这样,第二遍寻找新链表当前节点node的random时,只要在map里找原链表对应节点的value就可以了。
代码如下:
/*** Definition for singly-linked list with a random pointer.* class RandomListNode {* int label;* RandomListNode next, random;* RandomListNode(int x) { this.label = x; }* };*/ public class Solution {public RandomListNode copyRandomList(RandomListNode head) {if (head == null) {return head;}Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();RandomListNode copyHead = new RandomListNode(head.label);RandomListNode head1 = head;RandomListNode returnHead = copyHead;map.put(head1, copyHead);while(head1.next != null) {copyHead.next = new RandomListNode(head1.next.label);map.put(head1.next, copyHead.next);copyHead = copyHead.next;head1 = head1.next;}copyHead = returnHead;head1 = head;while(copyHead != null) {copyHead.random = map.get(head1.random);copyHead = copyHead.next;head1 = head1.next;}return returnHead;} }
上面的解法应该是比较容易理解的。Google到网友还有另一种比较好的解法,可以只花额外的O(1)空间,使用同样O(n)的时间。
这么做:
1. 在原来链表的每个节点后,插入一个一模一样的复制节点。这样copy的next顺序就自然出来了。比如1-1'-2-2'-3-3'
2. 将每个复制的节点的random指向它前一个节点的random的下一个节点
3. 按next的顺序抽出所有复制的节点,形成新的链表并返回。
代码如下
/*** Definition for singly-linked list with a random pointer.* class RandomListNode {* int label;* RandomListNode next, random;* RandomListNode(int x) { this.label = x; }* };*/ public class Solution {public RandomListNode copyRandomList(RandomListNode head) {if (head == null) {return head;}RandomListNode returnHead = head;//在原链表的每个节点后都插入一个新节点while(head != null) {RandomListNode newNode = new RandomListNode(head.label);newNode.next = head.next;head.next = newNode;head = head.next.next;}//将每个复制的节点的random指向它前一个节点的random的nexthead = returnHead;while(head != null) {if(head.random != null) {head.next.random = head.random.next;}head = head.next.next;}//抽出每个复制的节点,返回head = returnHead;returnHead = returnHead.next;while(head != null) {RandomListNode newNode = head.next;head.next = head.next.next;//这个边界条件一定要注意,否则原链表只有一个节点的时候会越界if(newNode.next != null) {newNode.next = newNode.next.next;}head = head.next;}return returnHead;} }
可以看出,上面的思想,同样是要用某种方法在新复制的链表里找到每个节点的random节点,方法依然是要依赖于原链表。但是思路非常巧妙,值得体会。
转载于:https://www.cnblogs.com/NickyYe/p/4379237.html
Copy List with Random Pointer相关推荐
- leetcode -day8 Copy List with Random Pointer Single Number I II
五一中间断了几天,开始继续... 1. Copy List with Random Pointer A linked list is given such that each node cont ...
- 138. Copy List with Random Pointer
/** 138. Copy List with Random Pointer * 2016-5-22 by Mingyang* 要遍历两次,第一次用来找到所有的next节点,并且把新旧节点全部存在ha ...
- LeetCode 138. Copy List with Random Pointer
LeetCode 138. Copy List with Random Pointer 参考链接:http://www.cnblogs.com/grandyang/p/4261431.html Sol ...
- 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] Copy list with random pointer 对带有任意指针的链表深度拷贝
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode OJ - Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- LeetCode || Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
最新文章
- Ctrl+shift+f不起作用的原因
- 【问链财经-区块链基础知识系列】 第三十二课 从区块链溯源来看农产品链的设计
- 《ASP.NET Core 微服务实战》译者序
- LeetCode 1177. 构建回文串检测(前缀和)
- 作者:牟少敏,博士,山东农业大学教授。
- idea连接mysql怎么生成实体类_IDEA链接数据库自动生成实体类
- Qt应用程序只运行一个实例
- 网络大学计算机统考缺考,2020网络教育统考缺考后会有什么影响
- 首都师范大学数学专业考研试题参考解答
- 明日之后哪个服务器人最多_明日之后,末日寻宝活动上线,远星城宝箱位置攻略...
- 【转载】Vue 2.x 实战之后台管理系统开发(二)
- 【收藏】华三交换机配置方法及操作实例
- PcShare2005代码阅读(1)
- mysql修改校对集_关于MySQL字符集和校对集问题
- 【Ubuntu】硬盘读写速度测试
- 小米智能插排内部结构
- plsql/developer12如何放大字体
- sinon spy_Sinon教程:使用嘲弄,间谍和存根进行JavaScript测试
- 盘点美国最出人意料的20种高薪职业
- 安装及调用Matlab程序可能存在的问题解决办法
热门文章
- c语言用链表编写简单程序,C语言单链表简单实现(简单程序复杂化)
- linux 后端的进程,linux后台启动进程
- python2读取excel中文处理,【Python】【源码】利用Python读取Excel文件-续
- html body 边框,html – 如何在表的tbody / thead部分创建边框?
- android 多手指触摸屏,AIR Android:关于多点触摸
- php后端如何搭建socket服务,从php做一个简单的socket服务器流程
- elementui可编辑单元格_关于遥感解译点室内解译编号的读取编辑方法
- python硬件交互_Python操作系统库说明,pythonos,笔记
- java 对象调用_java 对象调用
- mysql索引底层图_MySQL索引底层数据结构