当前位置:网站首页>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)
}
}
边栏推荐
- Record a case of using WinDbg to analyze memory "leakage"
- ClickHouse(03)ClickHouse怎么安装和部署
- 星环科技数据安全管理平台 Defensor重磅发布
- JDBC reads a large amount of data, resulting in memory overflow
- EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
- Numerical calculation method chapter8 Numerical solutions of ordinary differential equations
- Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
- 第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
- 第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
- OpenShift常用管理命令杂记
猜你喜欢

记录Pytorch中的eval()和no_grad()

Sophon AutoCV:助力AI工业化生产,实现视觉智能感知

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

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

吳恩達團隊2022機器學習課程,來啦

buuctf-pwn write-ups (9)

JVM third talk -- JVM performance tuning practice and high-frequency interview question record

Sophon KG升级3.1:打破数据间壁垒,解放企业生产力

About Estimation with Cross-Validation

Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
随机推荐
Introduction to Resampling
Operation before or after Teamcenter message registration
数值计算方法 Chapter8. 常微分方程的数值解
如何获取飞机穿过雷达两端的坐标
[use electron to develop desktop on youqilin]
小林coding的内存管理章节
【在优麒麟上使用Electron开发桌面应】
OpenShift常用管理命令杂记
从XML架构生成类
Logical words in Articles
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
Star ring technology data security management platform defender heavy release
小白入门NAS—快速搭建私有云教程系列(一)[通俗易懂]
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
【在優麒麟上使用Electron開發桌面應】
Introduction to VC programming on "suggestions collection"
Delete some elements in the array
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
模拟百囚徒问题