当前位置:网站首页>《力扣刷题计划》复制带随机指针的链表
《力扣刷题计划》复制带随机指针的链表
2022-07-05 17:55:00 【是小鱼儿哈】
每一个结点有三个属性:
链表的样子大概是这样:
注意这里我们复制的结点是引用类型,即我们复制的不只是结点的地址,我们需要复制的是旧结点。
比如一个这样的原链表
我们的复制不是又生成一个和上面一模一样的链表出来,我们复制的是有着原来链表结构的新的链表
新的链表的每个结点所对应的值可能和原来链表的值相同,但next和random的肯定是不同的(但所对应的关系是相同的)
为了在新链表中持和原来链表中各个结点的对应关系,我们需要用Map来记录
比如上图中
代码展示:
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
Node cur = head;
Map<Node, Node> map = new HashMap<>(); // 如果用TreeMap里面放的元素一定是可比较的,只能用HashMap
// 构建新旧结点的键值对关系,旧结点是Key,新结点是Value
while (cur != null) {
Node tmp = new Node(cur.val); // 构造新结点tmp,旧结点是cur
map.put(cur, tmp);
cur = cur.next;
}
cur = head;
// 按旧链表的结点对应关系构建新的结点,注意我们是深拷贝——即非引用类型可以直接把值拷贝出来,但对于引用类型我们拷贝的不只是地址值,还有该引用所对应的结点
while(cur != null) {
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
cur = cur.next;
}
return map.get(head); // 我们要返回的是新结点的head---及key(head) 所对应的 value值是map.get(head)
}
}
边栏推荐
- 【PaddleClas】常用命令
- Cmake tutorial Step3 (requirements for adding libraries)
- ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
- Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
- ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V
- Check namespaces and classes
- Leetcode daily question: merge two ordered arrays
- 星环科技数据安全管理平台 Defensor重磅发布
- RSE2020/云检测:基于弱监督深度学习的高分辨率遥感图像精确云检测
- Teamcenter 消息注册前操作或后操作
猜你喜欢
第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
寻找第k小元素 前k小元素 select_k
Image classification, just look at me!
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
Wu Enda team 2022 machine learning course, coming
About Estimation with Cross-Validation
ELK日志分析系统
Ten top automation and orchestration tools
Thesis reading_ Medical NLP model_ EMBERT
How awesome is the architecture of "12306"?
随机推荐
Cmake tutorial Step3 (requirements for adding libraries)
[JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business
Introduction to Resampling
Generate XML schema from class
matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图
Easynmon Usage Summary
“12306” 的架构到底有多牛逼?
LeetCode笔记:Weekly Contest 300
FCN: Fully Convolutional Networks for Semantic Segmentation
Image classification, just look at me!
Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
MATLAB中print函数使用
Redis Foundation
VC编程入门浅谈「建议收藏」
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
Cmake tutorial Step4 (installation and testing)
Delete some elements in the array
Huaxia Fund: sharing of practical achievements of digital transformation in the fund industry
Leetcode daily question: the first unique character in the string
Leetcode daily practice: rotating arrays