当前位置:网站首页>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)
}
}
边栏推荐
- 华夏基金:基金行业数字化转型实践成果分享
- Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
- LeetCode笔记:Weekly Contest 300
- Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection
- 深拷贝与浅拷贝【面试题3】
- 南京大学:新时代数字化人才培养方案探讨
- FCN: Fully Convolutional Networks for Semantic Segmentation
- Simulate the hundred prisoner problem
- 使用JMeter录制脚本并调试
- 隐私计算助力数据的安全流通与共享
猜你喜欢
隐私计算助力数据的安全流通与共享
星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
《力扣刷题计划》复制带随机指针的链表
南京大学:新时代数字化人才培养方案探讨
What are the changes in the 2022 PMP Exam?
含重复元素取不重复子集[如何取子集?如何去重?]
Elk log analysis system
寻找第k小元素 前k小元素 select_k
分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
Tupu software digital twin | visual management system based on BIM Technology
随机推荐
吳恩達團隊2022機器學習課程,來啦
Introduction to Resampling
matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图
Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
buuctf-pwn write-ups (9)
[use electron to develop desktop on youqilin]
兄弟组件进行传值(显示有先后顺序)
苹果手机炒股安全吗?打新债是骗局吗?
使用Jmeter虚拟化table失败
记录Pytorch中的eval()和no_grad()
JVM third talk -- JVM performance tuning practice and high-frequency interview question record
Writing writing writing
生词生词生词生词[2]
FCN: Fully Convolutional Networks for Semantic Segmentation
Crontab 日志:如何记录我的 Cron 脚本的输出
node_ Exporter memory usage is not displayed
Record a case of using WinDbg to analyze memory "leakage"
图像分类,看我就够啦!
【PaddleClas】常用命令
使用JMeter录制脚本并调试