LeetCode——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.
原题链接:https://oj.leetcode.com/problems/copy-list-with-random-pointer/
题目:给定一个链表,当中的每一个节点包括有一个额外的随机指针指向链表中的随意其它节点或空。
返回链表的一份深度复制。
思路:复制源链表中的每个节点到新链表中(仅仅考虑next)。复制随机指针关系到新链表中(仅仅考虑random.next),此时新链表的长度是源链表的2倍了。此时修正随机指针为正确的指向关系。
public RandomListNode copyRandomList(RandomListNode head) {
if(head == null)
return null;
RandomListNode p = head;
while(p != null){
RandomListNode copy = new RandomListNode(p.label);
copy.next = p.next;
p.next = copy;
p = copy.next;
}
p = head;
while(p != null){
if(p.random != null)
p.next.random = p.random.next;
p = p.next.next;
}
p = head;
RandomListNode newHead = head.next;
while(p != null){
RandomListNode tmp = p.next;
p.next = tmp.next;
if(tmp.next != null)
tmp.next = tmp.next.next;
p = p.next;
}
return newHead;
}
// Definition for singly-linked list with a random pointer.
class RandomListNode {
int label;
RandomListNode next, random;
RandomListNode(int x) {
this.label = x;
}
}
LeetCode——Copy List with Random Pointer的更多相关文章
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
- LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
问题: 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 ...
- [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]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
随机推荐
- ym——物联网入口之中的一个Android蓝牙4.0
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 假设还有同学不知道蓝牙4.0能够做什么请查看Android+蓝牙 4.0 将带来什么? ...
- Android Activity组件的启动过程
0.总图 1.总图中的第一步,Laucher主线程向ActivityManagerService进程发出START_ACTIVITY_TRANSACTION 如图:第一步 ~/Android/fram ...
- AWR系列之中的一个——AWR简单介绍
AWR的全称是Automatic Workload Repository(自己主动负载知识库). 它是通过对照两次快照的方式收集到统计信息.来生成txt或者html页面形式的报告. 通常,通过AWR报 ...
- wpf 全局异常捕获处理
/// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { ...
- Oracle DBA优化数据库性能心得
如今的优化己经向优化等待(waits)转型了,实际中性能优化最根本的出现点也都集中在IO,这是影响性能最主要的方面,由系统中的等待去发现Oracle库中的不足.操作系统某些资源利用的不合理是一个比较好 ...
- Linux操作系统下Oracle主要监控工具介绍
Oracle监控包括有效且完全地监控Oracle数据库的性能.可用性和使用率等统计量,还包括即时的错误通知和纠正措施,并提供全面的报表和图表.本文中主要介绍几种Linux操作系统下Oracle主要监控 ...
- Xcode的一些控制台命令
命令 解释 break NUM 在指定的行上设置断点 bt 显示所有的调用栈帧,该命令可用来显示函数的调用顺序 clear 删除设置在特定源文件.特定行上的断点,其用法为:clear FILENAME ...
- Node.js获取本机IP
function getIPAdress() { var interfaces = require('os').networkInterfaces(); for (var devName in int ...
- CorelDRAW X8制作金属质感3D立体按钮
本教程教您使用CorelDRAW X8制作金属质感3D立体按钮.绘图中主要应用渐变填充技巧为立体按钮表现物体质感和丰富的色彩变化,最后实现的效果也是不错的,是很实用的案例,教程难度一般,完成图如下: ...
- Long型转换成IP段String、StringIP段转换成Long型
/** 把long类型的Ip转为一般Ip类型:xx.xx.xx.xx * * @param ip * @return */ public static String getIpFromLong(Lon ...