Algorithms—138.Copy List with Random Pointer
2015-07-20 10:06
507 查看
思路:很简单的一题,next当作主线递归赋值,random作为支线附上即可。
/**
* 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) {
RandomListNode answer=null;
if (head!=null) {
answer=new RandomListNode(head.label);
}else {
return null;
}
if (head.random!=null) {
answer.random=new RandomListNode(head.random.label);
}
answer.next=copyRandomList(head.next);
return answer;
}
}
耗时:356ms,中游
/**
* 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) {
RandomListNode answer=null;
if (head!=null) {
answer=new RandomListNode(head.label);
}else {
return null;
}
if (head.random!=null) {
answer.random=new RandomListNode(head.random.label);
}
answer.next=copyRandomList(head.next);
return answer;
}
}
耗时:356ms,中游
相关文章推荐
- Linux加密类型及相关算法
- Expression : invalid operator < 解决方法
- Linux正则表达式awk讲解
- 《学习OpenCV》第四章课后题4
- twemproxy源码分析之五:zero copy
- Tomcat的Web部署
- 解决外网无法访问Linux主机上的tomcat问题
- getopt getopts
- 通过网页集中访问Docker容器
- Windows/Linux环境下模拟服务端口方法
- BASH操作环境的一些笔记
- linux scp命令(主机,服务器间复制文件)
- Photoshop做32位带Alpha通道的bmp图片
- 用户用浏览器访问一个网站的时候具体的过程与步骤
- hadoop2提交到Yarn: Mapreduce执行过程分析
- PuTTY + Xming 远程使用 Linux GUI
- PuTTY + Xming 远程使用 Linux GUI
- hadoop优化
- Apache Shiro与Spring Security学习
- hadoop-1.2.1添加节点的方法