当前位置:网站首页>《力扣刷题计划》复制带随机指针的链表
《力扣刷题计划》复制带随机指针的链表
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)
}
}
边栏推荐
- [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
- How to solve the error "press any to exit" when deploying multiple easycvr on one server?
- Which platform of outer disk gold is regular and safe, and how to distinguish it?
- Access the database and use redis as the cache of MySQL (a combination of redis and MySQL)
- How to improve the thermal management in PCB design with the effective placement of thermal through holes?
- 数值计算方法 Chapter8. 常微分方程的数值解
- Operation before or after Teamcenter message registration
- 小白入门NAS—快速搭建私有云教程系列(一)[通俗易懂]
- GFS distributed file system
- GFS分布式文件系统
猜你喜欢
Can communication of nano
How awesome is the architecture of "12306"?
Career advancement Guide: recommended books for people in big factories
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
GFS分布式文件系统
About Statistical Power(统计功效)
Image classification, just look at me!
隐私计算助力数据的安全流通与共享
mybash
Anaconda中配置PyTorch环境——win10系统(小白包会)
随机推荐
LeetCode笔记:Weekly Contest 300
Disorganized series
图像分类,看我就够啦!
About Estimation with Cross-Validation
Ten top automation and orchestration tools
Redis基础
Operation before or after Teamcenter message registration
Is it safe to open an account online? What is the general interest rate of securities financing?
Teamcenter 消息注册前操作或后操作
nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
PMP认证需具备哪些条件啊?费用多少啊?
Thesis reading_ Medical NLP model_ EMBERT
How can cluster deployment solve the needs of massive video access and large concurrency?
Ten capabilities that cyber threat analysts should have
Why is all (()) true and any (()) false?
mybash
pytorch yolov5 训练自定义数据
分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
小林coding的内存管理章节
苹果手机炒股安全吗?打新债是骗局吗?