当前位置:网站首页>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)
}
}
边栏推荐
- ITK Example
- Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
- 让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相
- Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection
- Leetcode daily question: the first unique character in the string
- Matlab built-in function how different colors, matlab subsection function different colors drawing
- node_ Exporter memory usage is not displayed
- rsync
- Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
- 爱因斯坦求和einsum
猜你喜欢

Unicode processing in response of flash interface

Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀

Neural network self cognition model

ISPRS2022/云检测:Cloud detection with boundary nets基于边界网的云检测

Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection

pytorch yolov5 训练自定义数据

About Statistical Power(统计功效)

Binder开辟线程数过多导致主线程ANR异常

Zabbix

Tupu software digital twin | visual management system based on BIM Technology
随机推荐
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
[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
文章中的逻辑词
Privacy computing helps secure data circulation and sharing
Configure pytorch environment in Anaconda - win10 system (small white packet meeting)
Leetcode exercise - 206 Reverse linked list
Tkinter window preload
How to improve the thermal management in PCB design with the effective placement of thermal through holes?
分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
兄弟组件进行传值(显示有先后顺序)
[utiliser Electron pour développer le Bureau sur youkirin devrait]
Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
Generate XML schema from class
第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
通过SOCKS代理渗透整个内网
Leetcode daily question: merge two ordered arrays
Notes on common management commands of openshift
node_exporter内存使用率不显示
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?