当前位置:网站首页>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];
}
边栏推荐
- EXE程序加密锁
- 时间序列-ARIMA模型
- The solution to the memory exhaustion problem when PHP circulates a large amount of data
- Samsung closes its last mobile phone factory in China
- Content ambiguity occurs when using transform:translate()
- Paper_ Book
- : 0xc0000005: an access conflict occurs when writing position 0x01458000 - to be solved
- Oracle 常用语句
- DEX and AMMS of DFI security
- jupyter 创建虚拟环境并安装pytorch(gpu)
猜你喜欢

The whereor method of TP5 has many conditions

The difference and use between get request and post request

Determine the exact type of data

第21回---第30回

编码技巧——全局异常捕获&统一的返回体&业务异常

Servlet基础知识点

DeFi安全之DEX与AMMs

Leetcode 226 flip binary tree (recursive)

Test novice learning classic (with ideas)

Excel extract duplicates
随机推荐
时间序列——使用tsfresh进行分类任务
Draw circuit diagram according to Verilog code
flume增量采集mysql数据到kafka
Wechat applet personal number opens traffic master
The method of inserting degree in word
编码技巧——全局日志开关
Chat skills
Web test learning notes 01
Paper_ Book
Axure install Icon Font Catalog
DRF use: get request to get data (small example)
将IAR工程文件夹转移目录后无法进入函数定义解决
Log management
Baidu picture copy picture address
Flume incrementally collects MySQL data to Kafka
新版jmeter函数助手不在选项菜单下-在工具栏中
JMeter5.3 及以后的版本jmeter函数助手生成的字符在置灰无法复制
Pointer summary
Introduction to JWT
2021-03-09