当前位置:网站首页>剑指Offer||:链表(简单)
剑指Offer||:链表(简单)
2022-06-28 07:23:00 【_索伦】
系列文章目录
剑指 Offer 06. 从尾到头打印链表
原题链接:从尾到头打印链表
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
题解:
利用虚拟头节点接受逆置后的链表,然后遍历链表,尾插到返回数组中。
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
ListNode* dummy = new ListNode();
dummy = ReverseList(head);
vector<int> ans;
while (dummy != nullptr) {
ans.emplace_back(dummy->val);
dummy = dummy->next;
}
return ans;
}
// 链表的逆置
ListNode* ReverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;
while (curr != nullptr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
};
剑指 Offer 35. 复杂链表的复制
原题链接:复杂链表的复制
请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。

题解:
我们首先将该链表中每一个节点拆分为两个相连的节点,例如对于链表 A→B→C,我们可以将其拆分为 A→A′→B→B′→C→C’。对于任意一个原节点 S,其拷贝节点 S′ 即为其后继节点。
这样,我们可以直接找到每一个拷贝节点 S′ 的随机指针应当指向的节点,即为其原节点 S 的随机指针指向的节点 T 的后继节点 T′。需要注意原节点的随机指针可能为空,我们需要特别判断这种情况。
当我们完成了拷贝节点的随机指针的赋值,我们只需要将这个链表按照原节点与拷贝节点的种类进行拆分即可,只需要遍历一次。同样需要注意最后一个拷贝节点的后继节点为空,我们需要特别判断这种情况。



class Solution {
public:
Node* copyRandomList(Node* head) {
if (head == nullptr) {
return nullptr;
}
for (Node* node = head; node != nullptr; node = node->next->next) {
Node* nodeNew = new Node(node->val);
nodeNew->next = node->next;
node->next = nodeNew;
}
for (Node* node = head; node != nullptr; node = node->next->next) {
Node* nodeNew = node->next;
nodeNew->random = (node->random != nullptr) ? node->random->next : nullptr;
}
Node* headNew = head->next;
for (Node* node = head; node != nullptr; node = node->next) {
Node* nodeNew = node->next;
node->next = node->next->next;
nodeNew->next = (nodeNew->next != nullptr) ? nodeNew->next->next : nullptr;
}
return headNew;
}
};
边栏推荐
- Detailed explanation of collection class methods____ (4) Judgment and assignment, etc
- What is a consistent hash? What scenarios can it be applied to?
- 腾讯下半年继续裁员,所有事业群至少缩减 10%,对此你怎么看?关注者
- MySQL installation steps - installing MySQL on Linux (3)
- How bacnet/ip gateway collects data of building centralized control system
- Vivo browser rapid development platform practice - Overview
- Huawei cloud computing physical node cna installation tutorial
- 面经---测试工程师web端自动化---大厂面试题
- LeetCode+ 51 - 55 回溯、动态规划专题
- 【C语言】详解 C 语言获取数组长度
猜你喜欢

什么是一致性哈希?可以应用在哪些场景?

"Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?

Application and Optimization Practice of redis in vivo push platform

分析 NFT 项目的 5 个指标

阿里云服务器创建快照、回滚磁盘

Kubernetes cluster lossless upgrade practice
![[C language] detailed explanation of C language to obtain array length](/img/cf/75c314bb622b8a1745f43cc07cb02e.png)
[C language] detailed explanation of C language to obtain array length

My MVVM open source project "travel epidemic prevention app" has been released

kubelet垃圾(退出的容器和未使用的镜像)回收源码分析

MySQL installation steps - installing MySQL on Linux (3)
随机推荐
分析 NFT 项目的 5 个指标
Compilation principles final review
R and RGL draw 3D knots
云原生(待更新)
Evolution of vivo push platform architecture
看似简单的光耦电路,实际使用中应该注意些什么?
Drawing animated bubble chart with R language
同花顺网上开户安全吗
Source code analysis of kubernetes' process of deleting pod
Sword finger offer II 091 Paint the house
Jetpack - defects of livedata component and Countermeasures
Force buckle 515 Find the maximum value in each tree row
okcc呼叫中心没有电脑的坐席能不能开展工作?
[C#][转载]furion框架地址和教程地址
The practice of event driven architecture in vivo content platform
File header information cross reference table
Leetcode+ 51 - 55 retrospective and dynamic planning topics
R 语言 Kolmogorov-Smirnov 检验 2 个样本是否遵循相同的分布。
kubelet垃圾(退出的容器和未使用的镜像)回收源码分析
Is it safe for flush to open an account online