138. Copy List with Random Pointer
来源:互联网 发布:膛线管55淘宝400元 编辑:程序博客网 时间:2024/05/16 18:33
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.
import java.util.ArrayList;import java.util.List;public class Solution {//List<Integer> randPos = new ArrayList<Integer>();List<RandomListNode> nowList = new ArrayList<RandomListNode>();List<RandomListNode> newList = new ArrayList<RandomListNode>(); public RandomListNode copyRandomList(RandomListNode head) { if(head == null)return null; RandomListNode dummy = new RandomListNode(0); RandomListNode tmp1 = head, tmp2 = dummy; while(tmp1 != null) { nowList.add(tmp1); tmp2.next = new RandomListNode(tmp1.label); newList.add(tmp2.next); tmp1 = tmp1.next; tmp2 = tmp2.next; } tmp1 = head; tmp2 = dummy.next; while(tmp1 != null) { int pos = nowList.indexOf(tmp1.random); if(pos != -1) tmp2.random = newList.get(pos); tmp1 = tmp1.next; tmp2 = tmp2.next; } return dummy.next; }}
0 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
- 野指针
- 我从编程总结的 22 个经验
- React Native 单位详细说明
- [BZOJ1552]robotic sort SPlay区间翻转
- Kafka集群搭建之路
- 138. Copy List with Random Pointer
- MATLAB中的Fdatool/Fvtool/Bertool简介
- Debug Assertion Failed ! List iterator not dereferencable
- android:打气筒功能View.inflate
- db2和oralce:用分析函数代替自连接
- viewpager即能实现滑动切换,又能点击切换的实现
- 收藏,C# WinForm 用override CreateParams 来重载窗体的一些特性(慎用!!!)
- SEO人员必学的关键词分析秘籍
- [51nod1594]Gcd and Phi