[leetcode]138. Copy List with Random Pointer复制带有随机指针的链表
public RandomListNode copyRandomList(RandomListNode head) {
/*
深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表,
每一个节点都是新建的,而不是指向就节点
这个题的难点在于:随机节点。
随机节点有可能指向后边还没有建立的节点,这就没法指。
方法一:一一对应地记录每个新旧节点的映射关系,在常规节点建立后,就去查哈希表,找到对应
新节点的旧节点的random,就是新节点的random
*/
if (head==null)
return null;
Map<RandomListNode,RandomListNode> map = new HashMap<>();
//新节点
RandomListNode n = new RandomListNode(head.label);
//记住:二叉树、链表这种前后有关联的数据结构,要迭代(注意是迭代方式)遍历的时候,一定要建立一个结构代表当前节点,往下遍历(递归看情况)
RandomListNode curN=n,curO=head;
map.put(curO,curN);
//先复制常规节点
while (curO.next!=null)
{
//复制节点
curN.next = new RandomListNode(curO.next.label);
curN = curN.next;
curO = curO.next;
//建立映射
map.put(curO,curN);
}
//复制random之前先初始化两个cur
curN = n;
curO = head;
while (curN!=null)
{
if (curO.random!=null)
{
//通过映射找到random
curN.random = map.get(curO.random);
}
curN = curN.next;
curO = curO.next;
}
return n;
}
[leetcode]138. Copy List with Random Pointer复制带有随机指针的链表的更多相关文章
- [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] 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 对带有任意指针的链表深度拷贝
A linked list is given such that each node contains an additional random pointer which could point t ...
- 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现
题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...
- leetcode 138. Copy List with Random Pointer复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
- 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 ----- java
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
原题地址 非常巧妙的方法,不需要用map,只需要O(1)的额外存储空间,分为3步: 1. 先复制链表,但是这个复制比较特殊,每个新复制的节点添加在原节点的后面,相当于"加塞"2. ...
随机推荐
- LeetCode 041 First Missing Positive
题目要求:First Missing Positive Given an unsorted integer array, find the first missing positive integer ...
- python核心高级学习总结5--------python实现线程
在代码实现上,线程的实现与进程的实现很类似,创建对象的格式都差不多,然后执行的时候都是用到start()方法,与进程的区别是进程是资源分配和调度的基本单位,而线程是CPU调度和分派的基本单位.其中多线 ...
- MySQL索引(一)索引基础
索引是数据库系统里面最重要的概念之一.一句话简单来说,索引的出现其实是为了提高数据查询的效率,就像书的目录一样. 常见模型 索引的出现是为了提高查询效率,但是实现索引的方式却有很多种,这里就介绍三种常 ...
- 老猿学5G扫盲贴:3GPP规范中与计费相关的主要规范文档列表及下载链接
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 在<老猿学5G扫盲贴:3GPP规范中部分与计 ...
- PyQt编程实战:画出QScrollArea的scrollAreaWidgetContents内容部署层的范围矩形
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 在<PyQt(Python+Qt)学习随笔:QScrollArea滚动区域详解> ...
- PyQt(Python+Qt)学习随笔:QTreeView的标题表头header相关属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 在Qt Designer中,对于树型视图QTreeView,在属性在下面有专门一栏列出了跟 ...
- PyCharm中怎么将非当前工程文件的目录的文件加到当前工程中
在PyCharm已经建立工程文件的情况下,如果要将一个其他目录的文件导入到已有的工程中,唯一的方法如下: 通过File->Settings->Project->Project Str ...
- PHP代码审计分段讲解(9)
22 弱类型整数大小比较绕过 <?php error_reporting(0); $flag = "flag{test}"; $temp = $_GET['password' ...
- 【软件测试部署基础】maven的认识
最近部门分享测试环境部署相关内容,在同事的分享下,学到了很多新的知识点,也是我们在测试环境部署的时候非常重要的一些基本的知识点,当你系统的去了解了一下,你会发现后端在maven相关的点上有个清晰的了解 ...
- 团队作业5_测试与发布(Alpha版本)
Alpha版本测试报告 1.测试找出的bug(N个): (1)修复的Bug:很多个,主要是一些疏忽造成的,比如请求url写错导致数据加载不了.比较有意义的bug是因为使用redux,但是用户刷新后数据 ...