当前位置:网站首页>【138. 复制带随机指针的链表】
【138. 复制带随机指针的链表】
2022-06-22 19:41:00 【爱吃榴莲的喵星人】
一、题目描述
二、提供方便走读代码的图

三、题目代码
/** * Definition for a Node. * struct Node { * int val; * struct Node *next; * struct Node *random; * }; */
struct Node* copyRandomList(struct Node* head) {
struct Node* cur = head;
struct Node* copy;
//复制节点,插入到原节点和下个节点之间
while(cur)
{
copy=(struct Node*)malloc(sizeof(struct Node));
copy->val=cur->val;
copy->next=cur->next;
cur->next=copy;
cur=copy->next;
}
//根据原节点random,处理复制节点的random
cur = head;
while(cur)
{
copy=cur->next;
if(cur->random==NULL)
{
copy->random=NULL;
}
else
{
copy->random=cur->random->next;
}
cur=copy->next;
}
//复制节点解下来链接成一个新链表,恢复原链表链接关系
cur = head;
struct Node* newhead=NULL;
struct Node* newtail=NULL;
while(cur)
{
struct Node* next=cur->next->next;
if(newhead==NULL)
{
newhead= newtail=cur->next;
}
else
{
newtail->next=cur->next;
newtail=cur->next;
}
cur->next=next;
cur=next;
}
return newhead;
}
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
猜你喜欢

R language organdata dataset visualization

R语言AirPassengers数据集可视化

已解决:一個錶中可以有多個自增列嗎

软件测试——测试用例设计&测试分类详解

R 语言 UniversalBank.csv“ 数据分析

R language airpassengers dataset visualization
Solutions to Oracle system/ user locking

R 语言nutrient数据集的可视化

Introduction of neural network (BP) in Intelligent Computing

R language CO2 dataset visualization
随机推荐
Code to image converter
84-我对网传<52 条 SQL 语句性能优化策略>的一些看法
R language Midwest dataset visualization
密码学系列之:PKI的证书格式表示X.509
程序员必看的学习网站
How to calculate yoy and mom in MySQL
Lora technology -- Lora signal changes from data to Lora spread spectrum signal, and then from RF signal to data through demodulation
R语言midwest数据集可视化
UnityEditor 编辑器脚本执行菜单
【剑指Offer】面试题44.数字序列中的某一位数字
Introduction of neural networks for Intelligent Computing (Hopfield network DHNN, CHNN)
Stochastic Adaptive Dynamics of a Simple Market as a Non-Stationary Multi-Armed Bandit Problem
百家讲坛 雍正十三年(下部)
Easyclick update Gallery
华为云发布拉美互联网战略
Feign FAQ summary
mysql8.0忘记密码的详细解决方法
迅睿CMS 自定义数据接口-php执行文件代码
90-最近优化过的几套Oracle数据库回顾
R语言organdata 数据集可视化


