Leetcode - CopyWithRandomList
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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- ansible的inventory文件含义
默认文件为/etc/ansible/hosts 例如 [test] web.yinzhipeng.com dhcp ansible_ssh_host=172.16.18.195 1.中括号中的名字代表 ...
- python模块打包方法
http://www.jb51.net/article/92789.htm 一 首先将模块的目录结构整理如下: VASPy/ ├── LICENSE ├── MANIFEST ├── MANIFEST ...
- selenium 定位元素方式大全
starts-with 顾名思义,匹配一个属性开始位置的关键字 contains 匹配一个属性值中包含的字符串 text() 匹配的是显示文本信息,此处也可以用来做定位用 eg //input[sta ...
- openssl/ssl.h file not found
sample/le-proxy.c:33:10: fatal error: 'openssl/ssl.h' file not found mac下,在安装某些软件的时候提示如上错误,但是mac已经安装 ...
- Windows下cwRsyncServer双机连续同步部署
下载cwRsyncServer服务器端与客户端的安装文件:服务端下载:cwRsyncServer_4.0.5_Installer.zip客户端下载:cwRsync_4.0.5_Installer.zi ...
- PHP empty()函数说明---用了N遍了就是记不住
从表面上看,很容易误解empty()函数是判断字符串是否为空的函数,其实并不是,我也因此吃了很多亏. empty()函数是用来测试变量是否已经配置.若变量已存在.非空字符串或者非零,则返回 false ...
- ECSHOP热门搜索关键词随机显示
实现ECSHOP热门搜索关键词随机显示,需要修改ECSHOP模板和ECShOP程序,按照步骤修改即可. 一.打开 include/lib_main.php 文件,找到下面这段代码,删除之(大概在165 ...
- 2017.5.1 使用fat jar插件来打包有引用外部jar包的项目
如果在程序开发时用到了第三方提供的API.jar包或者其他附属资源.在导出并生成项目的jar文件时,必须将第三方的文件一并导出,否则无法正确运行. 可以使用fat jar插件,下载地址:http:// ...
- spring 配置多数据源 (可行)
可以看到AbstractRoutingDataSource获取数据源之前会先调用determineCurrentLookupKey方法查找当前的lookupKey,这个lookupKey就是数据源标识 ...
- 【HTML 元素】标记文字
1.用基本的文字元素标记内容 先看显示效果: 对应HTML代码: <!DOCTYPE html> <html lang="en"> <head> ...