当前位置:网站首页>剑指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;
}
};
边栏推荐
- 什么是EC鼓风机(ec blower fan)?
- Wechat applets - basics takes you to understand the life cycle of applets (I)
- 同花顺网上开户安全吗
- Leetcode+ 51 - 55 retrospective and dynamic planning topics
- "Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?
- kubernetes删除pod的流程的源码简析
- Force buckle 515 Find the maximum value in each tree row
- linux下修改mysql端口号
- 阿里云服务器创建快照、回滚磁盘
- [ thanos源码分析系列 ]thanos query组件源码简析
猜你喜欢

C语言教程大全

Top 25 most popular articles on vivo Internet technology in 2021

vite2.9 中指定路径别名

我的MVVM开源项目《出行防疫App》已发布

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

BACnet/IP網關如何采集樓宇集中控制系統數據

How bacnet/ip gateway collects data of building centralized control system

Construction and exploration of vivo database and storage platform

What is a consistent hash? What scenarios can it be applied to?

golang gin框架进行分块传输
随机推荐
Comment la passerelle BACnet / IP recueille - t - elle les données du système central de contrôle des bâtiments?
自动化测试的生命周期是什么?
金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
kubelet垃圾(退出的容器和未使用的镜像)回收源码分析
What is EC blower fan?
MySQL master-slave replication, detailed configuration, create unable to connect processing prompt
Cloud native (to be updated)
DOM parsing of XML file case code sentence by sentence analysis
Rust FFI programming - libc crate
Pfizer's new Guankou medicine has entered the Chinese market, and the listing of relevant products of domestic pharmaceutical enterprises is just around the corner
Face to face experience --- test engineer web side automation --- interview questions for large factories
以动态规划的方式求解最长回文子串
okcc呼叫中心没有电脑的坐席能不能开展工作?
BACnet/IP網關如何采集樓宇集中控制系統數據
vite2.9 中指定路径别名
实时数据库 - 笔记
MySQL installation steps - Linux configuration file JDK installation (II)
C language tutorial
Server body 18: understanding and thinking of UDP reliable transmission (feeling from reading Yunfeng blog)
golang gin框架进行分块传输