[LeetCode]Copy List with Random Pointer
来源:互联网 发布:男生虚胖的原因知乎 编辑:程序博客网 时间:2024/06/05 11:32
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.
利用hashMap存储,原始的node作为key,new的node作为value,这样就建立了新旧链表的以一一对应关系;/** * 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 {Map<RandomListNode,RandomListNode> map = new HashMap<>();public RandomListNode copyRandomList(RandomListNode head) {RandomListNode scan = head;RandomListNode res = new RandomListNode(0);RandomListNode dummy = res;while(scan!=null){RandomListNode rln = new RandomListNode(scan.label);map.put(scan, rln);dummy.next = rln;scan = scan.next;dummy = dummy.next;}RandomListNode scan2 = head;RandomListNode dummy2 = res.next;while(scan2!=null){if(scan2.random==null){dummy2.random = null;}else{dummy2.random = map.get(scan2.random);}dummy2 = dummy2.next;scan2 = scan2.next;}return res.next;}}
0 0
- [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
- LeetCode(138)Copy List with Random Pointer
- [LeetCode] - Copy List with Random Pointer
- 项目总结之触摸问题分析
- 图的拓扑排序
- Android 扫描二维码实现网页登录
- JaxWS+Spring实现WebService
- android studio gradle 配置修改
- [LeetCode]Copy List with Random Pointer
- 黑马程序员-OC的面向对象
- 导入开源库到基于Android Studio构建的项目中
- 【转载】H.264中NAL、Slice与frame意思及相互关系
- tiny6410按键驱动(一)---驱动框架
- 编写函数模拟掷骰子的游戏(两个骰子)。第一次掷的时候,如果点数之和为7或11则获胜;如果点数之和为2、3或12则落败;其他情况下的点数之和称为“目标”,游戏继续。在后续的投掷中,如果玩家再次掷出“目标
- WebServices测试之三-----suds测试WEBSERVICES
- kinect的各种事情。。。
- Binary Tree Level Order Traversal (Java)