当前位置:网站首页>Copy the linked list with random pointer in the "Li Kou brush question plan"
Copy the linked list with random pointer in the "Li Kou brush question plan"
2022-07-05 18:11:00 【It's a little fish】

Each node has three attributes :

The list looks like this :

Note that the node we copied here is a reference type , That is, we copy not only the address of the node , What we need to copy is the old node .
For example, such an original linked list

Our replication does not generate another linked list exactly like the above , What we copy is a new linked list with the original linked list structure
The value corresponding to each node of the new linked list may be the same as that of the original linked list , but next and random It must be different ( But the corresponding relationship is the same )

In order to maintain the corresponding relationship with each node in the original linked list in the new linked list , We need to use Map To record
Like in the picture above

Code display :
/*
// 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<>(); // If you use TreeMap The elements inside must be comparable , Only use HashMap
// Build the key value pair relationship between the old and new nodes , Old node is Key, The new node is Value
while (cur != null) {
Node tmp = new Node(cur.val); // Construct a new node tmp, Old node is cur
map.put(cur, tmp);
cur = cur.next;
}
cur = head;
// Build a new node according to the node correspondence of the old linked list , Note that we are deep copy —— That is, non reference types can directly copy values , But for reference types, we copy more than address values , And the node corresponding to the reference
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); // What we want to return is the new node head--- And key(head) The corresponding value The value is map.get(head)
}
}
边栏推荐
- Multithreading (I) processes and threads
- 彻底理解为什么网络 I/O 会被阻塞?
- 兄弟组件进行传值(显示有先后顺序)
- Sophon autocv: help AI industrial production and realize visual intelligent perception
- To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
- Gimp 2.10 tutorial "suggestions collection"
- 第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
- ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V
- 记一次使用Windbg分析内存“泄漏”的案例
- Numerical calculation method chapter8 Numerical solutions of ordinary differential equations
猜你喜欢
![Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]](/img/b2/d019c3f0b85a6c0d334a092fa6c23c.png)
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]

Neural network self cognition model

分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载

南京大学:新时代数字化人才培养方案探讨

How awesome is the architecture of "12306"?

华夏基金:基金行业数字化转型实践成果分享

Wu Enda team 2022 machine learning course, coming

Can communication of nano

What are the changes in the 2022 PMP Exam?

模拟百囚徒问题
随机推荐
Compared with the loss of Wenxin, the performance is improved a lot
星环科技数据安全管理平台 Defensor重磅发布
Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
GFS distributed file system
Leetcode daily question: the first unique character in the string
ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V
使用JMeter录制脚本并调试
What are the requirements for PMP certification? How much is it?
nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
rsync
Check namespaces and classes
matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图
Cmake tutorial step1 (basic starting point)
MATLAB中print函数使用
检查命名空间和类
Eliminate the writing of 'if () else{}'
破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台
Crontab 日志:如何记录我的 Cron 脚本的输出
VC编程入门浅谈「建议收藏」