当前位置:网站首页>JZ35 复杂链表的复制
JZ35 复杂链表的复制
2022-04-23 02:40:00 【Rosita.】
题目链接:复杂链表的复制_牛客题霸_牛客网
注意点:
主要是考虑新旧节点的对应关系,以及random为空或者指向自己的情况
/*
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;
//定义新链表的头指针
RandomListNode* newhead = new RandomListNode(pHead->label);
//po指向旧链表的头,pn指向新链表的头
RandomListNode* po = pHead, *pn = newhead;
//哈希用于记录旧节点对应的新节点是否已经开辟,并且存储旧链表与先链表的地址
unordered_map<RandomListNode*, RandomListNode*> map;
//第一次遍历.保存每一个节点新旧的映射关系
while(po){
map[po] = new RandomListNode(po->label);
po = po->next;
}
po = pHead;
//第二次遍历给每个节点的random和next成员赋值
while(po){
pn->next = map[po->next];
pn->random = map[po->random];
po = po->next;
pn = pn->next;
}
return newhead;
}
};
版权声明
本文为[Rosita.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/pure_dreams/article/details/124348051
边栏推荐
- SO库依赖问题
- JDBC JDBC
- C # import details
- The usage and difference of * and & in C language and the meaning of keywords static and volatile
- Execute external SQL script in MySQL workbench and report error
- Suggestion: block reference sorting is in the order of keywords
- Applet canvas canvas half ring
- Six very 6 computer driver managers: what software is good for driver upgrade? Recommended by the best computer driver management software abroad
- 5W of knowledge points
- [NK] Niuke monthly race 48 D
猜你喜欢

Modify the content of MySQL + PHP drop-down box

下载正版Origin Pro 2022 教程 及 如何 激 活

全局、獨享、局部路由守衛

Real math problems in 1958 college entrance examination

Applet canvas canvas half ring

001_ Redis set survival time

Jupyter for local and remote access to ECS

Yes, from today on, our fans can participate in Netease data analysis training camp for free!

Interpretation of the future development of smart agriculture

The usage and difference of * and & in C language and the meaning of keywords static and volatile
随机推荐
If 404 page is like this | daily anecdotes
Develop a chrome plug-in from 0 (2)
魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
Talk about current limiting
下载正版Origin Pro 2022 教程 及 如何 激 活
Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
Want to experience homekit smart home? Why don't you take a look at this smart ecosystem
Machine learning (Zhou Zhihua) Chapter 14 probability graph model
win查看端口占用 命令行
Day 3 of learning rhcsa
C # import details
012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
Hack the box optimum
Efficient music format conversion tool Music Converter Pro
Yes, from today on, our fans can participate in Netease data analysis training camp for free!
Day18 -- stack queue
013_ Analysis of SMS verification code login process based on session
全局、獨享、局部路由守衛
Day 4 of learning rhcsa
Niuke hand speed monthly race 48 C (I can't understand the difference. It belongs to yes)