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.

解题思路:

我们在Java for LeetCode 133 Clone Graph题中做过图的复制,本题和图的复制十分类似,JAVA实现如下:

    public RandomListNode copyRandomList(RandomListNode head) {
if (head == null)
return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode node = new RandomListNode(head.label);
RandomListNode headTemp = head, nodeTemp = node;
map.put(head, node);
while (headTemp.next != null) {
nodeTemp.next = new RandomListNode(headTemp.next.label);
map.put(headTemp.next, nodeTemp.next);
headTemp = headTemp.next;
nodeTemp = nodeTemp.next;
}
headTemp = head;
nodeTemp = node;
while (headTemp!= null) {
if(map.containsKey(headTemp.random))
nodeTemp.random=map.get(headTemp.random);
headTemp = headTemp.next;
nodeTemp = nodeTemp.next;
}
return node;
}

最新文章

  1. Unity依赖注入使用
  2. Hadoop.2.x_时间服务器搭建(CentOs6.6)
  3. winform最小化后隐藏到右下角,单击或双击后恢复 .
  4. &lt;math.h&gt;与&lt;float.h&gt;
  5. 图片上传并显示(兼容ie),图片大小判断
  6. Mysql大小写敏感的问题 --转
  7. 文件操作IO流
  8. (五)认识Android中的Service
  9. JS左侧菜单-03
  10. 完善chrome翻译插件ChaZD,支持有道智云api
  11. 洛谷P2221 [HAOI2012]高速公路
  12. SQL Server 连接 MySQL
  13. Android打包遇到的那些坑
  14. Oracle启用scott用户
  15. Android中本地广播的实现
  16. 使用 Navicate 连接 Oracle9i 数据库
  17. 比较全git的.ignore文件配置
  18. maven 错误列表
  19. JAVA-Servlet高级应用
  20. IDEA安装以及项目初始化

热门文章

  1. linux系统故障分析与排查
  2. SparkStreaming和Drools结合的HelloWord版
  3. js 快速排序
  4. 升级Jekyll 3.0
  5. vue.js+koa2项目实战(二)创建 HeadBar 组件
  6. 【Python】继承
  7. Xutils的get请求后,总是返回同样数据的问题解决方式
  8. Chrome 插件 CrxMouse 去除后门优化版
  9. VueJS自定义过滤器:new Vue({filters:{filter1:function(){}....}})
  10. C语言重要概念汇总