当前位置:网站首页>I 刷题 I — 复制带随机指针的链表
I 刷题 I — 复制带随机指针的链表
2022-06-24 20:12:00 【MT_125】
目录
138. 复制带随机指针的链表 - 力扣(LeetCode)
https://leetcode.cn/problems/copy-list-with-random-pointer/

细节点:
1、每个节点中,额外含有一个指针,随机指向原链表中的任意一个节点(包括NULL)
2、深拷贝:将原链表的进行一次拷贝,拷贝链表中的结构要与原链表保持一致。
其中除原始节点顺序不变外,拷贝后的每个节点中的随机指针的指向与原链表节点 中的一 一对应(包括随机指针random)。
解题方法:
1. 首先:将每个拷贝节点连接在原链表后,如下图:

这样就可以用一个很妙的式子: copy->random = prev->random->next ;
prev记录的是,原链表的节点地址 。
2.然后:遍历拷贝链表,将拷贝节点中 random 按原链表的结构依次拷贝 。
3.因为题干要求返回拷贝链表的头节点地址,所以我们还要将拷贝链表与原链表 “ 解开 ” ,如下:

代码实现:
Node* copyRandomList(Node* head) {
Node* pphead = head;
if(head == NULL)
return NULL;
Node* Next = head->next;
//将copy节点连接到原链表
for(int i = 0;head!=NULL;head = Next)
{
Next = head->next;
Node* NewNode = (Node*)malloc(sizeof(Node));
head->next = NewNode;
NewNode->val = head->val;
NewNode->next = Next;
}
//将copy节点的random按原链表结构拷贝
Node* NEWhead = pphead->next;
head = pphead;
Node* prev = pphead;
Node* temp = NEWhead;
for(;prev!=NULL;)
{
if(prev->random!=NULL)
{
temp->random = prev->random->next;
}
else
{
temp->random = NULL;
}
prev = temp->next;
if(prev == NULL)
break;
temp = temp->next->next;
}
//将拷贝链表与原链表解开,并将原链表复原
prev = pphead;
for(temp = NEWhead;temp->next!=NULL;temp = temp->next)
{
prev->next = temp->next;
prev = temp->next;
temp->next = temp->next->next;
}
prev->next = NULL;
return NEWhead;
}
边栏推荐
- UE4 WebBrowser chart cannot display problems
- Transition from digitalization to intelligent manufacturing
- Modstart: embrace new technologies and take the lead in supporting laravel 9.0
- 移动安全工具-jar
- Several ways for wechat applet to jump to the page are worth collecting
- 实现mnist手写数字识别
- A website for programmers with a monthly salary of 30K
- Scrollview height cannot fill full screen
- D omit parameter name
- Registration method of native method in JNI
猜你喜欢
Is it so difficult to calculate the REM size of the web page according to the design draft?

Use and click of multitypeadapter in recycleview

百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑

Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...

ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’

Usage of ViewModel and livedata in jetpack

2021-11-07
@mysql

Working principle analysis of kubernetes architecture core components

Kubernetes 架构核心组件工作原理解析
随机推荐
redis + lua实现分布式接口限流实现方案
Paint rounded rectangle
Network request -volley
The problem of multiple callback of video ads stimulated by applets (offcolse problem)
Use of navigation and navigationui
Android SQLite database
Mobile security tool apktool
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
Tiktok wallpaper applet, starlight wallpaper applet version 2.0, upgraded version
D does not require opapply() as a domain
融合模型权限管理设计方案
传输层 以字节为单位的滑动窗口技术
UE4 WebBrowser chart cannot display problems
Usage of ViewModel and livedata in jetpack
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!
2021-04-18
ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
Encryption and encoding resolution
Alternative to log4j
Collection of software testing and game testing articles