当前位置:网站首页>Leetcode 随即链表的深拷贝
Leetcode 随即链表的深拷贝
2022-07-28 05:18:00 【zhengyawen666】
题目链接:力扣
题目要求:
给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。
构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/copy-list-with-random-pointer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:
初步思路:将源节点一个一个尾插到新的链表中,之后通过遍历寻找random。但是由于malloc的特性,所以在新的链表中,每一个结点的地址都是不确定的,即使找到了原链表中结点对应的random,也很难体现在新链表中。如果对比值的话,出现两个相同的值就无法判断。如果对比地址又很难找到并且效率低下。

难点:找到对应的random的关系。怎么找到对应的random的关系呢?可以将新链表的每个结点插入到原链表中,这样一来,通过原链表random的对应关系,那么在新链表中也很容易找到了。

新思路:
①首先将原链表一个一个拷贝,然后将拷贝的每一个结点插入到原链表的两个结点中间。便于之后确定新链表的random
②将拷贝的新链表的random确定下来。copy->random=cur->random->next(找到了对应的结点之后需要在新链表中体现)
③将新链表拿下来尾插,之后恢复原链表的关系。
代码:
class Solution {
public:
Node* copyRandomList(Node* head) {
struct Node*cur=head;
while(cur)
{
struct Node*copy=(struct Node*)malloc(sizeof(struct Node));
copy->val=cur->val;
copy->next=cur->next;
cur->next=copy;
cur=copy->next;
}//插入
cur=head;
while(cur)
{struct Node*copy=cur->next;
if(cur->random==NULL)
{
copy->random=NULL;
}
else
{
copy->random=cur->random->next;
}
cur=copy->next;
}//找random
cur=head;
struct Node*copytail,*copyhead=NULL;
while(cur)
{
struct Node*copy=cur->next;
struct Node*next=copy->next;
if(copytail==NULL)
{
copyhead=copytail=copy;
}
else
{
copytail->next=copy;
copytail=copytail->next;
}
cur->next=next;
cur=next;
}
return copyhead;
}
};边栏推荐
- Problems encountered when the registry service Eureka switches to nocas
- Learning of image enhancement evaluation index -- structural similarity SSIM
- Advanced multi threading: the underlying principle of synchronized, the process of lock optimization and lock upgrade
- Mutual conversion between latex and word
- 深度学习医学图像模型复现
- Pytorch uses hook to get feature map
- Edge calculation kubeedge+edgemash
- visio如何快速生成相同的图案,生成图像矩阵
- 冶金物理化学复习 ---- 气固反应动力学
- 导出excel,生成多个sheet页,并命名
猜你喜欢

latex和word之间相互转换

Advanced multithreading: the role and implementation principle of volatile

You must configure either the server or JDBC driver (via the ‘serverTimezone)

Review of Metallurgical Physical Chemistry - gas liquid phase reaction kinetics

ResNet结构对比

Redis' bloom filter

冶金物理化学复习 --- 化学反应动力学基础

RESNET structure comparison

Advanced multi threading: the underlying principle of synchronized, the process of lock optimization and lock upgrade

冶金物理化学复习 --- 冶金反应动力学基础与多相反应动力学特征
随机推荐
openjudge:找出全部子串位置
MySQL adds sequence number to query results
Distillation model diagram
Learning of image enhancement evaluation index -- structural similarity SSIM
Use of IO streams
Review of Metallurgical Physical Chemistry - gas liquid phase reaction kinetics
You must configure either the server or JDBC driver (via the ‘serverTimezone)
CentOS7安装MySQL5.7
论文写作用词
The way of deep learning thermodynamic diagram visualization
When using deep learning training image, the image is too large for segmentation training prediction
Advanced multithreading: the role and implementation principle of volatile
Writing methods of scientific research papers: add analysis and discussion in the method part to explain their contributions and differences
Centos7 install MySQL 5.7
RESNET structure comparison
【单例模式】懒汉模式的线程安全问题
数据库面试
The essence of dynamic convolution
深度学习医学图像模型复现
Review of metallurgical physical chemistry ---- gas solid reaction kinetics