当前位置:网站首页>剑指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;
}
};
边栏推荐
- 什么是一致性哈希?可以应用在哪些场景?
- 未来互联网人才还稀缺吗?哪些技术方向热门?
- What is EC blower fan?
- Server body 18: understanding and thinking of UDP reliable transmission (feeling from reading Yunfeng blog)
- Llvm and clang
- Rust FFI programming - libc crate
- es6箭头函数中return的用法
- ABAP skill tree
- C语言教程大全
- Huawei cloud computing physical node cna installation tutorial
猜你喜欢
Alibaba cloud server creates snapshots and rolls back disks
vite2.9 中指定路径别名
Construction and exploration of vivo database and storage platform
Compilation principles final review
Open62541 import nodeset file directly
一个小工具可以更快的写爬虫
面经---测试工程师web端自动化---大厂面试题
Wechat applets - basics takes you to understand the life cycle of applets (I)
我的MVVM开源项目《出行防疫App》已发布
腾讯下半年继续裁员,所有事业群至少缩减 10%,对此你怎么看?关注者
随机推荐
看似简单的光耦电路,实际使用中应该注意些什么?
面经---测试工程师web端自动化---大厂面试题
Jinshan cloud team shared | 5000 words to understand how Presto matches with alluxio
Will Internet talents be scarce in the future? Which technology directions are popular?
Mysql8.0 and mysql5.0 accessing JDBC connections
"Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?
[C#][转载]furion框架地址和教程地址
Pytorch RNN learning notes
ABAP 技能树
NDK 交叉编译
This keyword details
基金的投资交易与结算
网传互联网公司加班表,排名第一的没悬念
File header information cross reference table
Kubernetes cluster lossless upgrade practice
C language tutorial
LeetCode+ 66 - 70 高精度、二分专题
Leetcode+ 66 - 70 high precision, two sub topics
Wechat applets - basics takes you to understand the life cycle of applets (I)
Hack the box:routerspace