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; }
}

Leetcode - CopyWithRandomList的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. Google Protocol Buffer安装编译及使用

    近期玩了玩谷歌的Protocol Buffer.以下就简介下 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准.眼下已经正在使用的 ...

  2. 转换流的使用(InputStreamReader,OutputStreamWriter)

    我们如何设置字节输入流.字节输出流的编码和解码格式? 在字节输入流(InputStream).字节输出流(OutputStream)中,并没有提供设置编码和解码格式的方法. InputStream的A ...

  3. docker开发之pyudev模块用法

    一.实现功能:获取docker_id #docker数据源: [root@docker scripts]# docker ps -a CONTAINER ID IMAGE COMMAND CREATE ...

  4. 新人补钙系列教程之:Molehill底层API中最重要的Context3D

    Context3D,是一个三维空间的处理环境,负责创建并处理三维对象的各个要素如顶点.片段.透视等等,并将处理的结果使用AGAL(Adobe图形汇编语言)上传给显卡进行运算,运算结果最终被回传给CPU ...

  5. Hadoop 变更磁盘的方法总结

    背景说明HDFS文件系统使用一段时间后,可能会出现磁盘空间不足或是磁盘损坏的现象,此时需要对DataNode节点的磁盘进行扩充或是更换,本文对操作流程做一个简单的总结 操作步骤 挂载硬盘 添加硬盘的操 ...

  6. [Other] An Overview of Arrays and Memory

    One integer takes 32bit in memory, 1 byte = 8bits, therefore one integer takes 4 bytes. Now let's as ...

  7. JAVA Eclipse如何设置点击按钮切换图片

    右击图片文件夹,新建一个Android XML文件   设置文件的名称,注意这个新建的xml文件就是会被用作按钮的background属性的,所以名字不要太奇怪,设置Root Element为sele ...

  8. python raise assert

    class MyException(Exception): def __init__(self,error_msg): self.error_msg=error_msg def __str__(sel ...

  9. ionic - 运行起来

    更新时间: 2018-8-1 (首次更新) 1.首先下载python(至于为什么安装,看截图) https://www.python.org/downloads/release/python-370/ ...

  10. (五)Thymeleaf标准表达式之——[7->8]条件表达式& 默认表达式

    2.7 条件表达式 模板名称:condition-express.html <1>a ? b:c  (if then:else) <2>a?c (if else) 条件表达式( ...