Algorithm: Iterate and copy the original list first. For the random pointer, just copy the value from the original list first. And use a map to store each node's old address and its corresponding new address. After the iteration,
we can replace the value of the random pointer based on the map we get.

Mistakes I make:

(1) Beware when head == null.

(2) Forget during the iteration: node = node.next;

public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head == null)
return null;
RandomListNode newHead = new RandomListNode(head.label);
newHead.random = head.random;
RandomListNode node = newHead;
Map<RandomListNode,RandomListNode> addrRef = new HashMap<RandomListNode,RandomListNode>(); addrRef.put(head, node);
head = head.next; while(head != null)
{
RandomListNode tnode = new RandomListNode(head.label);
tnode.random = head.random;
node.next = tnode; addrRef.put(head, tnode); head = head.next;
node = tnode;
} node = newHead;
while(node != null)
{
node.random = addrRef.get(node.random);
node = node.next;
} return newHead; }
}

最新文章

  1. Android一个大众化的设置界面
  2. hdu 5902 Seam Carving
  3. framework 安装出错 1603
  4. information_schema.key_column_usage 学习
  5. My97DaePicker 用js实现文本框日期相减求天数
  6. FPGA知识大梳理(四)FPGA中的复位系统大汇总
  7. css中换行的几种方式
  8. Codeforces Round #390 (Div. 2)
  9. Debian上安装java
  10. 后缀自动机模板(SPOJ1811)
  11. 实验二Java面向对象程序设计
  12. DNS Brand
  13. caffe添加自己的数据输入层
  14. 用PSCP在Windows和Linux之间相互传输文件
  15. 20145226夏艺华 《Java程序设计》实验报告一
  16. nexus在linux上搭建
  17. html中的事件属性
  18. 基于Python的接口测试框架实例
  19. LeetCode OJ:Number of Islands(孤岛计数)
  20. RHEL因为selinux设置失误,无法重启问题。(centos适用)

热门文章

  1. django10 使用自定义标签配置说明
  2. Linux修改SSH端口
  3. 【共享单车】—— React后台管理系统开发手记:员工管理之增删改查
  4. 飘逸的python - __get__ vs __getattr__ vs __getattribute__以及属性的搜索策略
  5. Spring3的quartz定时任务
  6. 基于WPF系统框架设计(2)-Fluent Ribbon之HelloWorld
  7. Java 实例
  8. Linux组件封装(五)一个生产者消费者问题示例
  9. Solution to Triangle by Codility
  10. Dephi泛型generic的应用