leetcode做题总结,题目Copy List with Random Pointer 2013/10/03
来源:互联网 发布:知乎 加拿大ee 编辑:程序博客网 时间:2024/06/18 15:38
这个题目自己一开始没有想出来,复制一个链表容易,但是随机指针需要随机定位节点,所以可以把新老节点交叉连在一起。设置完随机指针后断开即可。
public RandomListNode copyRandomList(RandomListNode head) { if(head==null)return null; RandomListNode tmp; RandomListNode h=head; while(head!=null){ tmp = new RandomListNode(head.label); tmp.next=head.next; head.next=tmp; head=tmp.next; } head=h; while(head.next!=null){ if(head.random!=null)head.next.random=head.random.next; head=head.next.next; if(head==null)break; } RandomListNode s1=h; RandomListNode s2=h.next; h=s2; while(s2.next!=null){ s1.next=s2.next; s2.next=s2.next.next; s1=s1.next; s2=s2.next; } s1.next=null; return h; }
Update 2015/08/20: 面试的时候问到过这道题,于是在熟练地用上面的解法做完之后,面试官问能不能不修改原始链表。于是只能用hashmap重新写,思路就是使用map的K-V储存新-旧节点即可。
0 0
- leetcode做题总结,题目Copy List with Random Pointer 2013/10/03
- leetcode题目:Copy List with Random Pointer
- [leetcode][list] Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- LeetCode: Copy List with Random Pointer
- [LeetCode] Copy List with Random Pointer
- [leetcode]Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- leetcode Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- LeetCode题解:Copy List with Random Pointer
- [LeetCode]Copy List with Random Pointer
- leetcode Copy List with Random Pointer
- leetcode-Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- LeetCode | Copy List with Random Pointer
- [LeetCode] Copy List with Random Pointer
- LeetCode OJ:Copy List with Random Pointer
- 软件设计的七大原则
- ActionScript3游戏中的图像编程(连载十一)
- malloc,colloc,realloc内存分配,动态库,静态库的生成与调用
- pat1074_备份
- ZOJ Problem Set - 1512 (Water Treatment Plants)
- leetcode做题总结,题目Copy List with Random Pointer 2013/10/03
- 黑马程序员 java学习笔记——面向对象2:继承
- int *p,cons int *p,int const *p,int * const p,const int * const p,int const * const p的区别
- javascript类工厂的实现!
- 用于读取爬虫后的文件
- 四层和七层负载均衡的区别
- 算法-
- 单线程实现检索当当网泄露的1GB用户数据
- sprintf,求字符串长度