当前位置:网站首页>剑指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;
}
};
边栏推荐
- R language ggmap visual cluster
- MMR rearrangement (similarity is calculated by editing distance and repeatability)
- flutter 实现摇一摇功能
- 一个小工具可以更快的写爬虫
- kubernetes删除pod的流程的源码简析
- [ thanos源码分析系列 ]thanos query组件源码简析
- Block transmission by golang gin framework
- 以动态规划的方式求解最长回文子串
- Source code analysis of kubernetes' process of deleting pod
- Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
猜你喜欢

SQL statement optimization steps (1)
![[ thanos源码分析系列 ]thanos query组件源码简析](/img/e4/2a87ef0d5cee0cc1c1e1b91b6fd4af.png)
[ thanos源码分析系列 ]thanos query组件源码简析

goland IDE和delve调试位于kubernetes集群中的go程序

Leetcode+ 51 - 55 retrospective and dynamic planning topics

mysql57 zip文件安装

ABAP skill tree

Kubelet garbage collection (exiting containers and unused images) source code analysis

Compile configuration in file

GoLand IDE and delve debug Go programs in kubernetes cluster

How bacnet/ip gateway collects data of building centralized control system
随机推荐
Solving the longest palindrome substring by dynamic programming
ABAP 技能树
Vivo browser rapid development platform practice - Overview
文件头信息对照表
R语言绘制 ggplot2 季节性图
剑指offer II 091.粉刷房子
Hack the box:routerspace
Path alias specified in vite2.9
Practice and exploration of vivo live broadcast application technology
No suspense about the No. 1 Internet company overtime table
R language ggmap visual cluster
NDK cross compilation
R 语言 ggmap
金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
代码提交规范
在idea中,get和set方法爆红可能是没有安装Lombok插件
SQL statement optimization steps (1)
A gadget can write crawlers faster
LeetCode+ 66 - 70 高精度、二分专题
Spark 离线开发框架设计与实现