当前位置:网站首页>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;
}
边栏推荐
- redis + lua实现分布式接口限流实现方案
- What is test development? Can you find a job at this stage?
- 2021-11-05
- MySQL log management
- In the process of enterprise development, I found that a colleague used the select * from where condition for update
- Tiktok wallpaper applet source code
- Previous basic review
- Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
- 大厂高频软件测试面试题和答案都帮你准备好啦,备战金九银十
- Wallpaper applet wechat applet
猜你喜欢
Custom control - round dot progress bar (imitating one key acceleration in security guard)
Wx applet jump page
2019 summary and 2020 outlook
移动安全工具-jar
More pictures | explain the Nacos parameters in detail!
Single blind box removal, social blind box and friend blind box program source code
iNFTnews | 国内NFT发展仅限于数字藏品吗?
Punch smart spirit 1. The brand is attractive. What is the strength of the product?
[redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly
Applet opening traffic master
随机推荐
5-minute NLP: summary of 3 pre training libraries for rapid realization of NER
Transition from digitalization to intelligent manufacturing
Databinding quick start (still using findviewbyid?)
Kubernetes 架构核心组件工作原理解析
传输层 以字节为单位的滑动窗口技术
Some examples of MgO operating database in go
[redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly
Xcode preview displays a bug in the content of the list view and its solution
A website for programmers with a monthly salary of 30K
Previous basic review (link)
Redis + Lua implementation of distributed interface current limiting
【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS
Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
Alternative to log4j
[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
Intensive reading of thinking about markdown
[interview question] the difference between instancof and getclass()
Single blind box removal, social blind box and friend blind box program source code
Tiktok wallpaper applet source code
Use of navigation and navigationui