【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
思路:第一遍正常复制链表,同一时候用哈希表保存链表中原始节点和新节点的相应关系,第二遍遍历链表的时候,再复制随机域。
这是一种典型的空间换时间的做法,n个节点,须要大小为O(n)的哈希表,同一时候时间复杂度能够减少到O(n)。
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) {
return head;
}
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode res = null;
RandomListNode taiListNode = null;
RandomListNode cur = head;
while (cur != null) {
if (res == null) {
res = new RandomListNode(cur.label);
res.next = res.random = null;
taiListNode = res;
map.put(head, res);
}
else{
taiListNode.next = new RandomListNode(cur.label);
taiListNode = taiListNode.next;
map.put(cur, taiListNode); }
cur = cur.next;
}
taiListNode.next = null;
cur = head;
taiListNode = res;
while (cur != null) {
taiListNode.random = (RandomListNode)map.get((RandomListNode)cur.random);
cur = cur.next;
taiListNode = taiListNode.next;
}
return res;
}
}
【LeetCode】Copy List with Random Pointer的更多相关文章
- 【leetcode】Copy List with Random Pointer (hard)
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【Leetcode】【Hard】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【LEETCODE OJ】Copy List with Random Pointer
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...
- Java for LeetCode 138 Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- leetcode 138. Copy List with Random Pointer ----- java
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- c++11 学习
#include <iostream> // std::cout #include <functional> // std::ref #include <thread&g ...
- OJ刷题---简单password破解
题目要求: 输入代码: #include<iostream> #include <cstdio> #include <cstring> using namespac ...
- 《Javascript权威指南》学习笔记之十九--HTML5 DOM新标准---处理文档元信息和管理交互能力
一.了解DOM 1.DOM是Document Object Model的缩写,即文档对象类型,是文档在内存中的表示形式,是一个应用程序接口,定义了文档的逻辑结构以及一套訪问和处理文档的方法. 2.HT ...
- PHP-PHP5.3及以上版本中检查json格式的方法
function is_json($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } j ...
- JBoss jar包冲突及jar加载顺序
http://blog.163.com/javaee_chen/blog/static/17919507720116149511489/将一个完整的.war包部署到Jboss容器中,启动后报如下错误: ...
- Android 4.4KitKat AudioTrack 流程分析
Android Audio 系统的主要内容: AudioManager:这个主要是用来管理Audio系统的,需要考虑整个系统上声音的策略问题,例如来电话铃声,短信铃声等,主要是策略上的问题. Audi ...
- CentOS erlang安装、emqtt
安装erlang 如果未安装以后程序,请先安装依赖 $sudo yum install gcc gcc-c++ glibc-devel make ncurses-devel openssl-dev ...
- HTTPSConnectionPool(host='xxxxx', port=443): Max retries exceeded with url:xxxxxxxx (Caused by NewConnectionError('<urllib3.connect,Max retries exceeded with ,(Caused by NewConnectionError
HTTPSConnectionPool(host='f6ws-sha8re-o88k.s3.ama66zaws.com', port=443): Max retries exceeded with u ...
- MySQL怎样存储IP地址
为什么要问如何存储IP 首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗? 其实做任何程序设计都要在功能实现的基础上最大限度的优化性能.而数据库设计是程序设计中不 ...
- memcache stats命令详解
参数不算多,我们来启动一个Memcache的服务器端: /usr/local/bin/memcached -d-m 10 -u root-l 192.168.0.200-p 12000-c 256- ...