当前位置:网站首页>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
边栏推荐
- 想用Mac学习sql,主要给自己个充足理由买Mac听听意见
- Rhcsa day 1 operation
- Yes, from today on, our fans can participate in Netease data analysis training camp for free!
- JVM class loader
- windows MySQL8 zip安装
- 下载正版Origin Pro 2022 教程 及 如何 激 活
- Water diversion into chengluo Valley p1514
- wordpress 调用指定页面内容详解2 get_children()
- 接口请求时间太长,jstack观察锁持有情况
- Lighting LED of IAR embedded development stm32f103c8t6
猜你喜欢
随机推荐
谷雨
MySQL / SQL Server判断表或临时表存在则删除
SQL server2019无法下载所需文件,这可能表示安装程序的版本不再受支持,怎么办了
接口请求时间太长,jstack观察锁持有情况
Web learning record (medium)
PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]
Renesas electronic MCU RT thread development and Design Competition
Rhcsa second day operation
windows MySQL8 zip安装
机器学习(周志华) 第十四章概率图模型
JVM class loader
TypeScript(1)
002_ Redis_ Common operation commands of string type
使用Go语言构建Web服务器
Fashion MNIST dataset classification training
定了,今日起,本号粉丝可免费参与网易数据分析培训营!
007_ Redis_ Jedis connection pool
Interim summary (Introduction + application layer + transportation layer)
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
Six very 6 computer driver managers: what software is good for driver upgrade? Recommended by the best computer driver management software abroad