刷题 链表

embedded/2024/10/10 23:13:22/

面试经典150题 - 链表

141. 环形链表

在这里插入图片描述

class Solution {
public:bool hasCycle(ListNode *head) {ListNode* slow = head, *fast = head;while (fast != nullptr && fast->next != nullptr) {slow = slow->next;fast = fast->next->next;if (slow == fast) {return true;}}return false;}
};

142. 环形链表 II

在这里插入图片描述

在这里插入图片描述

class Solution {
public:ListNode *detectCycle(ListNode *head) {ListNode* slow = head, *fast = head;while (fast != nullptr && fast->next != nullptr) {slow = slow->next;fast = fast->next->next;if (slow == fast) {slow = head;while (slow != fast) {slow = slow->next;fast = fast->next;}return slow;}}return nullptr;}
};

2. 两数相加 - 虚拟头节点 + 加法进位

在这里插入图片描述

class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode* dummy_head = new ListNode(0);ListNode* cur = dummy_head, *cur_1 = l1, *cur_2 = l2;int sum = 0, carry = 0;while (cur_1 || cur_2 || carry != 0) {sum = 0;if (cur_1) {sum += cur_1->val;cur_1 = cur_1->next;}if (cur_2) {sum += cur_2->val;cur_2 = cur_2->next;}sum += carry;carry = sum / 10;cur->next = new ListNode(sum % 10);cur = cur->next;}ListNode* head = dummy_head->next;delete dummy_head;dummy_head = nullptr;return head;}
};

21. 合并两个有序链表 - 虚拟头节点

在这里插入图片描述

class Solution {
public:ListNode* mergeTwoLists(ListNode* node_1, ListNode* node_2) {ListNode* dummy_head = new ListNode();ListNode* cur = dummy_head;while (node_1 != nullptr && node_2 != nullptr) {if (node_1->val < node_2->val) {cur->next = node_1;node_1 = node_1->next;} else {cur->next = node_2;node_2 = node_2->next;}cur = cur->next;}cur->next = node_1 ? node_1 : node_2; // 直接连接剩余部分ListNode* head = dummy_head->next;delete dummy_head;dummy_head = nullptr;return head;}
};

138. 随机链表的复制 - 深拷贝 - 哈希表存储对应关系

在这里插入图片描述

class Solution {
public:// 本题相当于 对 链表进行深拷贝Node* copyRandomList(Node* head) {unordered_map<Node*, Node*> copy_hash;Node* cur = head;// 先 根据节点的值 新建拷贝,并通过哈希表关联while (cur) {copy_hash[cur] = new Node(cur->val);cur = cur->next;}cur = head;// 根据哈希表 将 拷贝节点 连接起来 (next, random)while (cur) {copy_hash[cur]->random = copy_hash[cur->random];copy_hash[cur]->next = copy_hash[cur->next];cur = cur->next;}return copy_hash[head];}
};

http://www.ppmy.cn/embedded/125594.html

相关文章

新个性化时尚解决方案!Prompt2Fashion:自动生成多风格、类型时尚图像数据集。

今天给大家介绍一种自动化生成时尚图像数据的方法Prompt2Fashion。 首先创建了一组描述&#xff0c;比如“适合婚礼的休闲风格服装”&#xff0c;然后用这些描述来指导计算机生成图像。具体来说&#xff0c;他们使用了大型语言模型来写出这些服装的描述&#xff0c;接着将这些描…

音频进阶学习三——离散时间信号与系统

文章目录 前言一、离散时间信号1.基本信号2.离散时间信号的分类3.离散时间信号的简单运算4.单位脉冲在运算中的作用 二、离散时间系统1.什么是离散时间系统2.离散系统的分类 总结 前言 前面博主介绍了信号中的连续时间信号和离散时间信号&#xff0c;数字信号也是离散时间信号…

python 实现Tarjan 用于在有向图中查找强连通分量的算法

Tarjan 用于在有向图中查找强连通分量的算法介绍 Tarjan算法是一种用于在有向图中查找强连通分量的高效算法&#xff0c;由Robert Tarjan在1972年提出。强连通分量是指在有向图中&#xff0c;如果从顶点u到顶点v以及从顶点v到顶点u都存在一条路径&#xff0c;那么顶点u和顶点v…

mysql迁移到达梦数据库报错:参数不兼容

1: 这个错误可能是某个字段‘定义超长’&#xff0c;尝试&#xff1a; 2: 如果还报错&#xff0c;指定和mysql同版本驱动

python 生成exe之后,想更换图标的方法

在 Python 中&#xff0c;使用 PyInstaller 来将你的脚本打包成 .exe 文件&#xff0c;并且想要更换生成的可执行文件的图标&#xff0c;可以使用 icon 参数来指定一个 .ico 文件。 以下是一般的步骤&#xff1a; 1、准备图标文件&#xff1a; 你需要一个 .ico 格式的图标文件…

Leetcode 135-分发糖果

1&#xff09;所有人分1个candy 2&#xff09;从左往右查看&#xff0c;若满足左规则&#xff0c;令 candy[i] candy[i - 1] 1 3&#xff09;从右往左查看&#xff0c;若满足右规则&#xff0c;令 candy[j] Math.max(candy[j 1] 1,candy[j])&#xff0c;取最大值是为了在满足…

门窗对象检测系统源码分享[一条龙教学YOLOV8标注好的数据集一键训练_70+全套改进创新点发刊_Web前端展示]

门窗对象检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Computer Vision …

YOLOv10改进策略【注意力机制篇】| MCAttention 多尺度交叉轴注意力

一、本文介绍 本文记录的是基于MCA注意力模块的YOLOv10目标检测改进方法研究。普通的轴向注意力难以实现长距离交互&#xff0c;不利于捕获分割任务中所需的空间结构或形状&#xff0c;而MCA注意力模块通过构建了两个并行轴向注意力之间的交互&#xff0c;更有效地利用多尺度特…