当前位置:网站首页>Replication of complex linked list
Replication of complex linked list
2022-07-27 16:25:00 【Cute rain】
Title Description : Please implement copyRandomList function , Copy a complex list . In complex linked list , Each node has one next The pointer points to the next node , One more random The pointer points to any node in the list or NULL;
Question : The copy of linked list can directly traverse the linked list , Create a new node implementation , there random What does the pointer do ?
Problem solving : As the title says , What is needed here is the replication of complex linked lists , there random It doesn't matter what the pointer does , The important thing is how to copy the random pointer correctly .
Ideas : Using the query characteristics of hash table , Consider building the key value mapping relationship between the original linked list node and the corresponding node of the new linked list , Then traverse the nodes of the new linked list next and random Reference points to . In other words, traverse both sides , The first time, only build new nodes , Build a new node for the second time next and cur.
The illustration :

Code implementation :
Node* copyRandomList(Node* head)
{
if (head == NULL)return NULL;
unordered_map<Node*, Node*>map;
Node* cur = head;
while (cur != NULL)
{
map[cur] = new Node(cur->val);
cur = cur->next;
}
cur = head;
while (cur != NULL)
{
map[cur]->next = map[cur->next];
map[cur]->random = map[cur->random];
cur = cur->next;
}
return map[head];
}
边栏推荐
- Axure 安装图标字体元件库
- Your password does not satisfy the current policy requirements (modify MySQL password policy setting simple password)
- TSMC's 6-nanometer manufacturing process will enter trial production in the first quarter of next year
- SolidWorks simulation curve attribute setting
- centos上mysql5.7主从热备设置
- flume增量采集mysql数据到kafka
- Enable shallow and deep copies+
- google chrome revercecaptcha广告屏蔽
- 第31回---第52回
- Personal perception of project optimization
猜你喜欢

ADAMS中转动整个模型

Web test learning notes 01

Leetcode25 question: turn the linked list in a group of K -- detailed explanation of the difficult questions of the linked list

DRF learning notes (IV): DRF view

Text capture picture (Wallpaper of Nezha's demon child coming to the world)

Introduction to JWT

Nacos

解决flink启动后无法正常关闭

Excel extract duplicates

Leetcode 226 flip binary tree (recursive)
随机推荐
Common tool classes under JUC package
Content ambiguity occurs when using transform:translate()
matlab legend用法
时间序列-ARIMA模型
Common Oracle statements
Boolean value
pdf 提取文字
4位数的随机数据
SolidWorks Simulation曲线图属性设定
解决flink启动后无法正常关闭
Const summary
Addition of large numbers
Chat skills
Samsung closes its last mobile phone factory in China
training on multiple GPUs pytorch
DRF learning notes (V): viewset
my_ls小结
201403-1
Flask connects to existing tables in MySQL database
2021-03-09