当前位置:网站首页>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;
}
边栏推荐
- D omit parameter name
- Custom animation (simulated win10 loading animation) - Optimization
- Use of file class filenamefilter & filefilter in io
- 2019 summary and 2020 outlook
- [redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly
- Apk decompiled method (not confused)
- ros(25):rqt_ image_ View reports an error unable to load plugin for transport 'compressed', error string
- QT(35)-操作EXCEL-QXlsx-QAxObject
- 断言(assert)的用法
- How can I persuade leaders to use DDD to construct the liver project?
猜你喜欢

JDBC - database connection

Apk slimming compression experience

大厂高频软件测试面试题和答案都帮你准备好啦,备战金九银十

Thermodynamic diagram display correlation matrix

Custom control - round dot progress bar (imitating one key acceleration in security guard)
Is it so difficult to calculate the REM size of the web page according to the design draft?

Discrete mathematics and its application detailed explanation of exercises in the final exam of spring and summer semester of 2018-2019 academic year
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
How can I persuade leaders to use DDD to construct the liver project?

Use of JMeter
随机推荐
融合模型权限管理设计方案
Mobile security tool apktool
C#和C 的CAN通信实验
Network request -volley
Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
Activity startup process
The drawableleft of the custom textview in kotlin is displayed in the center together with the text
无需显示屏的VNC Viewer远程连接树莓派
Simple collation of Web cache
通过kubernetes可视化界面(rancher)安装kibana
ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
VNC viewer remote connection raspberry pie without display
移动安全工具-apktool
Wallpaper applet wechat applet
2021-09-12
Single blind box removal, social blind box and friend blind box program source code
The basic principle and application of iterator and enhanced for
Scrollview height cannot fill full screen
Kibana installation via kubernetes visual interface (rancher)
在企业级开发过程中我发现有位同事用select * from where 条件 for update