当前位置:网站首页>JZ35 replication of complex linked list
JZ35 replication of complex linked list
2022-04-23 02:41:00 【Rosita.】
Topic link : Replication of complex linked list _ Niuke Tiba _ Cattle from
Be careful :
It mainly considers the corresponding relationship between old and new nodes , as well as random Empty or pointing to your own situation
/*
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
*/
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead) {
if(!pHead) return NULL;
// Define the head pointer of the new linked list
RandomListNode* newhead = new RandomListNode(pHead->label);
//po Point to the header of the old linked list ,pn Point to the head of the new list
RandomListNode* po = pHead, *pn = newhead;
// Hash is used to record whether the new node corresponding to the old node has been opened , And store the addresses of the old linked list and the first linked list
unordered_map<RandomListNode*, RandomListNode*> map;
// First traversal . Save the old and new mapping relationship of each node
while(po){
map[po] = new RandomListNode(po->label);
po = po->next;
}
po = pHead;
// The second traversal gives each node random and next Member assignment
while(po){
pn->next = map[po->next];
pn->random = map[po->random];
po = po->next;
pn = pn->next;
}
return newhead;
}
};
版权声明
本文为[Rosita.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230240052786.html
边栏推荐
- 类初始化和实例初始化面试题
- JDBC JDBC
- Fashion MNIST dataset classification training
- Synchronized锁及其膨胀
- Water diversion into chengluo Valley p1514
- 013_ Analysis of SMS verification code login process based on session
- 认识进程(多线程_初阶)
- 1215_ Hello world used by scons
- Servlet template engine usage example
- IAR embedded development stm32f103c8t6 Lighting LED
猜你喜欢

A domestic image segmentation project is heavy and open source!

高效音乐格式转换工具Music Converter Pro

Implementation of distributed scenario business operation log (based on redis lightweight)

SO库依赖问题

Practical combat of industrial defect detection project (II) -- steel surface defect detection based on deep learning framework yolov5

ROP Emporium x86_64 7~8题

打靶narak

Real math problems in 1958 college entrance examination

全局、独享、局部路由守卫

【unity3D】直播间滚动式弹幕效果
随机推荐
牛客手速月赛 48 C(差分都玩不明白了属于是)
Day 4 of learning rhcsa
在MySQL Workbench中执行外部的SQL脚本,报错
hack the box optimum靶机
[suggestion collection] hematemesis sorting out golang interview dry goods 21 questions - hanging interviewer-1
Step principle of logical regression in machine learning
Explain JS prototype and prototype chain in detail
[untitled]
005_ redis_ Set set
mysql function函数语法
leetcode 烹饪料理
一个国产图像分割项目重磅开源!
Rhcsa day 4 operation
[XJTU计算机网络安全与管理]第二讲 密码技术
MySQL / SQL Server判断表或临时表存在则删除
Huashu "deep learning" and code implementation: 01 Linear Algebra: basic concepts + code implementation basic operations
Intelligent agricultural management model
Day 3 of learning rhcsa
windows MySQL8 zip安装
Machine learning (Zhou Zhihua) Chapter 14 probability graph model