当前位置:网站首页>复制带随机指针的链表
复制带随机指针的链表
2022-08-04 02:46:00 【Kkkkvvvvvxxx】
前言
题目:
给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。构造这个链表的深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/copy-list-with-random-pointer
代码实现
struct Node
{
int val;
struct Node* next;
struct Node* random;
};
struct Node* copyRandomList(struct Node* head)
{
struct Node* copy = NULL;
struct Node* cur = head;
//复制结点
while (cur)
{
copy = (struct Node*)malloc(sizeof(struct Node));
//复制
copy->val = cur->val;
copy->next = cur->next;
cur->next = copy;
//
cur = copy->next;
}
//给random赋值
cur = head;
while (cur)
{
copy = cur->next;
if (cur->random == NULL)
{
copy->random = NULL;
}
else
{
copy->random = cur->random->next;
}
cur = cur->next->next;
}
//恢复链表,拆分复制的链表
struct Node* tail = NULL, * newhead = NULL;
cur = head;
while (cur)
{
copy = cur->next;
if (tail == NULL)
{
newhead = tail = copy;
}
else
{
tail->next = copy;
tail = tail->next;
}
//恢复链表
cur->next = copy->next;
//迭代
cur = copy->next;
}
return newhead;
}
分析
已知存在如下链表,我需要将该链表复制,然后返回复制后的链表的头指针!
First
首先我们看下图
我们先将链表的各个结点进行一个复制,然后将复制后的结点链接到原链表两两数据之间,如图所示,复制7结点之后,将复制后的结点插入到7和13之间即可,因此有如下代码;
struct Node* copy = NULL;
struct Node* cur = head;
//复制结点
while (cur)
{
//创造结点
copy = (struct Node*)malloc(sizeof(struct Node));
//复制
copy->val = cur->val;
copy->next = cur->next;
cur->next = copy;
//重新指向
cur = copy->next;
}
Next
在将链表复制后插入到原链表之后,我们接下来就需要对复制之后的结点中的random进行一个赋值,如图所示。

cur = head;
while (cur)
{
copy = cur->next;
if (cur->random == NULL)
{
copy->random = NULL;
}
else
{
copy->random = cur->random->next;
}
cur = cur->next->next;
}
cur是用来便利原链表的一个指针,每次前进两步。将cur的random所指向的next给copy的random即可。
Last
struct Node* tail = NULL, * newhead = NULL;
cur = head;
while (cur)
{
copy = cur->next;
if (tail == NULL)
{
newhead = tail = copy;
}
else
{
tail->next = copy;
tail = tail->next;
}
//恢复链表
cur->next = copy->next;
//迭代
cur = copy->next;
}
接下来就要将链表拆下来,然后对原链表进行还原;这个逻辑比较简单,再次不进行赘述!
边栏推荐
- STM8S105k4t6c---------------Light up LED
- activiti流程执行过程中,数据库表的使用关系
- esp8266-01s刷固件步骤
- 【原创】启动Win10自带的XPS/OXPS阅读器
- 多线程间的通信方式你知道几种?
- 【Playwright测试教程】5分钟上手
- Why use Selenium for automated testing
- QNX Hypervisor 2.2 user manual] 10.1 gm vdev options
- Snake game bug analysis and function expansion
- 董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白
猜你喜欢

MySQL高级-读写分离-分库分表

Zabbix set up email alert + enterprise WeChat alert

融云「音视频架构实践」技术专场【内含完整PPT】

阿里云国际版基于快照与镜像功能迁移云服务器数据

sqoop ETL tool

In the season of going overseas, the localization of Internet tips for going overseas

esp8266-01s刷固件步骤

Priority_queue element as a pointer, the overloaded operators

Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。

STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目
随机推荐
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
说说数据治理中常见的20个问题
Presto中broadcast join和partition join执行计划的处理过程
Deep Learning (3) Classification Theory Part
跨境电商看不到另一面:商家刷单、平台封号、黑灰产牟利
2022广东省安全员A证第三批(主要负责人)考试题库及模拟考试
共n级台阶,每次可以上1级或2级台阶,有多少种上法?
halcon自定义函数基本操作
安装postgis时报找不到“POSTGIS_VERSION”这个函数
怎样提高网络数据安全性
一文看懂推荐系统:召回04:离散特征处理,one-hot编码和embedding特征嵌入
一文看懂推荐系统:召回05:矩阵补充、最近邻查找,工业界基本不用了,但是有助于理解双塔模型
MallBook 助力SKT思珂特教育集团,立足变化,拥抱敏捷交易
织梦内核电动伸缩门卷闸门门业公司网站模板 带手机版【站长亲测】
2022.8.3-----leetcode.899
倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
pytorch应用于MNIST手写字体识别
2022年茶艺师(中级)考试试题模拟考试平台操作
STM8S-----选项字节
STM8S project creation (STVD creation) --- use COSMIC to create a C language project
