Copy List with Random Pointer
2015-10-27 21:37
375 查看
题目:
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存储原始结点和copy的结点的对应关系。
先把结点整个从头到尾进行复制。然后从头到尾复制它的random
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存储原始结点和copy的结点的对应关系。
先把结点整个从头到尾进行复制。然后从头到尾复制它的random
/** * 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 { public RandomListNode copyRandomList(RandomListNode head) { Map<RandomListNode,RandomListNode> map = new HashMap<>(); RandomListNode p = head; RandomListNode dummy = new RandomListNode(0); RandomListNode q = dummy; while(p!=null) { q.next = new RandomListNode(p.label); map.put(p,q.next); p = p.next; q = q.next; } p = head; q = dummy.next; while(p!=null) { q.random = map.get(p.random); p = p.next; q = q.next; } return dummy.next; } }
相关文章推荐
- 构建可调试的openfire插件
- centos6.5 jdk7 ant 64位安装
- stopping hbasecat: /tmp/hbase-root-master.pid: No such file or directory
- TransactionScope类的使用
- OpenGL
- hadoop学习笔记之<hadoop fs命令详解>
- 捕获内核的异常事件
- POJ3050 Hopscotch 【DFS】
- 常用的OpenCV函数速查
- 【Unified Auditing】统一审计的存储架构体系
- 如何查看自己电脑支持OpenGL core版本
- 自己写一个爬虫 copider
- hadoop学习笔记之<hadoop fs和hdfs dfs命令>
- CheungSSH运维自动化软件 批量管理上千台服务器
- opencv 将两张图片显示到一幅图片中
- 网站架构的演变之路(58同城)
- Jmeter之Bean shell使用(一)
- 制作loop档案
- [Linux学习笔记] Linux常用命令 - 权限管理命令
- 老王学linux-lamp-rpm