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 ...
随机推荐
- How To Use NSOperations and NSOperationQueues
Update 10/7/14: This tutorial has now been updated for iOS 8 and Swift; check it out! Everyone has h ...
- 关于各浏览器的cookie上限
IE6~IE6以下,每个域名最多20个cookie IE7及以上,每个域名最多50个cookie Firefox,每个域名最多50个cookie Opera,每个域名最多30个cookie Safar ...
- 矩阵十题【五】 VOJ1049 HDU 2371 Decode the Strings
题目链接:https://vijos.org/p/1049 题目大意:顺次给出m个置换,重复使用这m个置换对初始序列进行操作.问k次置换后的序列.m<=10, k<2^31. 首先将这m个 ...
- 【Docker】Docker管理平台 Rancher ---- 你应该学学Rancher是怎么做容器的管理的
Elasticsearch is a Lucene-based search engine developed by the open-source vendor, elastic. With pri ...
- 关于ElasticSearch默认窗口结果集参数max_result_window修改
在Linux服务器中执行如下命令 curl -XPUT http://192.168.46.163:9200/t_order/_settings -d '{ "index" : { ...
- 用word2vec对语料进行训练
在Linux上安装好word2vec, 进入trunk文件夹,把分词后的语料文件放在trunk文件夹内,执行:./word2vec -train tt.txt -output vectors.bin ...
- gzip gunzip压缩保留源文件的方法:
Linux压缩保留源文件的方法: gzip –c filename > filename.gz Linux解压缩保留源文件的方法: gunzip –c filename.gz > file ...
- mybatis like写法
name like concat(concat('%',#{name}),'%') name like concat('%',#{name},'%')
- 【Linux】Linux删除指定文件夹下面 名称不包含指定字符的文件
例如:现在文件夹home下面有以下数据文件列表 A_20171215.DAT B_20160630.DAT C_20170823.DAT 现在想删除不包含"20160630"这个字 ...
- MySQL查询缓存设置提高MySQL查询性能
首先看看MSYQL逻辑框架:图片来自高性能mysql 如果使用了QueryCache,当查询接收到一个和之前同样的查询,服务器将会从查询缓存中检索结果,而不是再次分析和执行相同的查询.这样就能大大提高 ...