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 ...
随机推荐
- easyui numberbox precision属性
//设置easyui numbox 最小值为0,保留2为小数 <input id="payPrice" type="text" name="pa ...
- java中map的应用
map是键值对的集合接口,需要存储两个字段有联系的字段时可以使用这个. 1.查找字符串数组重复次数最多的字符串的重复次数 思路:使用map键值对存储,key值存数字符串,value存储出现的次数 Ma ...
- 转:svn 更新指定文件夹
通常由于创建很多个branch和tag,当我们要去checkout指定tag和branch的时候,会不得不把整个branch/tag目录checkout出来.是不是有点傻??!!! 那么如何有选择ch ...
- hibernate学习系列-----(5)hibernate基本查询下篇:hibernate聚合函数、分组查询及命名查询
在上一篇中,大致学习了hibernate的基本查询:HQL基本查询,今天,继续昨天的步伐,继续学习hibernate的基本查询..... 1.hql聚合函数,先大致列一下hql的聚合函数有哪些吧: 在 ...
- VM虚拟机 Windows虚拟机中linux鼠标不能动怎么办
有一次vmware安装red hat linux后,进入x-windows界面,鼠标不能用,百思不得其解,因为自己的安装linux的过程中设置绝对是没有问题的啊,鼠标设置肯定是usb带滑轮,这个肯定没 ...
- ionic中actionsheet在安卓中显示样式问题
可以看到在浏览器上是正常的,在安卓上的样式没了 建议不要直接去动ionic的css文件,容易影响全局 方法:注释掉_action-sheet.sass中文件123行,针对安卓样式去写的样sass 保存 ...
- 嵌套矩形——DAG上的动态规划
有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.非常多问题都能够转化为DAG上的最长路.最短路或路径计数问题. 题目描写叙述: 有n个矩形,每一个矩 ...
- magento安装wordpress
说起来事实上很easy,如今简诉一下安装过程 1.到magento connect界面安装magento扩展,地址:http://www.magentocommerce.com/magento-con ...
- 获取web应用路径 // "/" 表示class 根目录
/** * 获取web应用路径 * @Description : 方法描述 * @Method_Name : getRootPath * @return * @return : String * @C ...
- 微信小程序 - 豆瓣同城
代码地址如下:http://www.demodashi.com/demo/12121.html 一.准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.com/ ...