LeetCode Copy List with Random Pointer
2016-02-29 15:15
453 查看
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.
题意:给出一个链表,包含next和random结点,作深拷贝
思路:链表的遍历操作
代码如下:
class RandomListNode
{
int label;
RandomListNode next, random;
RandomListNode(int x) {this.label = x;}
}
class Solution
{
public RandomListNode copyRandomList(RandomListNode head)
{
if (null == head) return null;
RandomListNode new_head = new RandomListNode(head.label);
RandomListNode current = head, new_current = new_head;
while (current != null)
{
if (current.next != null)
{
new_current.next = new RandomListNode(current.next.label);
}
if (current.random != null)
{
new_current.random = new RandomListNode(current.random.label);
}
current = current.next;
new_current = new_current.next;
}
return new_head;
}
}
Return a deep copy of the list.
题意:给出一个链表,包含next和random结点,作深拷贝
思路:链表的遍历操作
代码如下:
class RandomListNode
{
int label;
RandomListNode next, random;
RandomListNode(int x) {this.label = x;}
}
class Solution
{
public RandomListNode copyRandomList(RandomListNode head)
{
if (null == head) return null;
RandomListNode new_head = new RandomListNode(head.label);
RandomListNode current = head, new_current = new_head;
while (current != null)
{
if (current.next != null)
{
new_current.next = new RandomListNode(current.next.label);
}
if (current.random != null)
{
new_current.random = new RandomListNode(current.random.label);
}
current = current.next;
new_current = new_current.next;
}
return new_head;
}
}
相关文章推荐
- Linux分区
- 5分钟弄懂Docker!
- (总结)Linux下su与su -命令的本质区别
- Linux分区
- virtualbox5.0.8 centos6.7 mini 安装
- 基于消息的分布式架构
- 页面乱码解决
- linux下的C语言编程(总结篇)
- Linux的SOCKET编程详解
- linux ubuntu 14.0 修改文件上下左右出现ABCD
- MyBatis(3.2.3) - Configuring MyBatis using XML, Properties
- dubbo的监控中心::记录调用次数、响应时间?
- CentOS上PHP完全卸载
- 在linux 4.x内核中增加系统调用
- 网站上传到阿里云centos6.3显示不了
- centos 6 换源
- Auto property synthesis will not synthesizeproterty;it will be implemented by its superclass, use@dy
- 网站结构优化-慕课网
- ubuntu获得root用户权限,使用xshell连接!
- linux下如何获得特定字符串前后几行文本