138. Copy List with Random Pointer
来源:互联网 发布:变胖变瘦的软件 编辑:程序博客网 时间:2024/06/07 02:49
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.
code
注意某个节点没有next,但是有random的情况
/** * Definition for singly-linked list with a random pointer. * public class RandomListNode { * public int label; * public RandomListNode next, random; * public RandomListNode(int x) { this.label = x; } * }; */public class Solution { public RandomListNode CopyRandomList(RandomListNode head) { if(head==null) return head; RandomListNode clonehead = new RandomListNode(head.label); if(head.next!=null) clonehead.next = new RandomListNode(head.next.label); if(head.random!=null) clonehead.random = new RandomListNode(head.random.label); RandomListNode next = CopyRandomList(head.next); var cur = clonehead.next; if(next!=null){ cur.next = next.next; cur.random = next.random; } return clonehead; }}
阅读全文
2 0
- 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- [Leetcode] 138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- [leetcode] 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- leetcode.138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- LeetCode-138.Copy List with Random Pointer
- Leetcode 138. Copy List with Random Pointer
- [Leetcode]138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- [LeetCode] 138. Copy List with Random Pointer
- centos6.5配置静态ip
- 安卓libStagefright高危漏洞分析
- R实战:【常用函数】rnorm正态分布
- 河南OI2016·食物链
- 《HBase权威指南》读书笔记 第八章:架构,B+树
- 138. Copy List with Random Pointer
- C#学习-阶段总结一
- 51nod 1821 最优集合
- 串
- 【二分图匹配入门专题1】E
- 【Quartz】——CronTrigger 表达式的编写
- centos6下增加swap空间
- 环境变量中classpath 和 path区别
- 【思维导图训练1】--思维导图的基本概述