【leetcode】链表反转题目总结

server/2024/9/22 18:17:22/

206. 反转链表

全部反转

递归法和迭代法

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* reverseList(ListNode* head) {if (head == nullptr || head->next == nullptr) {return head;}// 1. 递归法// ListNode* last = reverseList(head->next);// head->next->next = head;// head->next = nullptr;// return last;// 2. 迭代法ListNode* pre = nullptr;ListNode* cur = head;while (cur != nullptr) {ListNode* next = cur->next;cur->next = pre;pre = cur;cur = next;}head->next = nullptr;return pre;}
};

25. K 个一组翻转链表

分组反转

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:// 反转区间[a,b)的元素,左闭右开ListNode* reverseAb(ListNode* a, ListNode* b) {ListNode* pre = nullptr;ListNode* cur = a;while (cur != b) {ListNode* next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}ListNode* reverseKGroup(ListNode* head, int k) {// base caseif (head == nullptr) {return nullptr;}ListNode* a = head;ListNode* b = head;for (int i = 0; i < k; ++i) {// 不足 k 个,不需要反转,base caseif (b == nullptr) {return head;}b = b->next;}// 反转前 k 个元素ListNode* newHead = reverseAb(a, b);// 递归反转后续链表并连接起来a->next = reverseKGroup(b, k);return newHead;}
};

24. 两两交换链表中的节点

其实就是两个一组反转链表,利用上个题目的方式:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:// 反转区间[a,b)的元素,左闭右开ListNode* reverseAb(ListNode* a, ListNode* b) {ListNode* pre = nullptr;ListNode* cur = a;while (cur != b) {ListNode* next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}ListNode* reverseKGroup(ListNode* head, int k) {// base caseif (head == nullptr) {return nullptr;}ListNode* a = head;ListNode* b = head;for (int i = 0; i < k; ++i) {// 不足 k 个,不需要反转,base caseif (b == nullptr) {return head;}b = b->next;}// 反转前 k 个元素ListNode* newHead = reverseAb(a, b);// 递归反转后续链表并连接起来a->next = reverseKGroup(b, k);return newHead;}ListNode* swapPairs(ListNode* head) {if (head == nullptr || head->next == nullptr) {return head;}return reverseKGroup(head, 2);}
};

递归法

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* swapPairs(ListNode* head) {if (head == nullptr || head->next == nullptr) {return head;}ListNode* newHead = head->next;ListNode* nextPairs = newHead->next;newHead->next = head;head->next = swapPairs(nextPairs);return newHead;}
};

迭代法

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* swapPairs(ListNode* head) {ListNode* dummy = new ListNode(-1, head);ListNode* node = dummy;while (node->next != nullptr && node->next->next != nullptr) {ListNode* node1 = node->next;ListNode* node2 = node->next->next;node->next = node2;node1->next = node2->next;node2->next = node1;node = node1;}return dummy->next;}
};

92. 反转链表 II

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:// 反转区间[a,b)的元素,左闭右开ListNode* reverseAb(ListNode* a, ListNode* b) {ListNode* pre = nullptr;ListNode* cur = a;while (cur != b) {ListNode* next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}ListNode* reverseBetween(ListNode* head, int left, int right) {ListNode* dummy = new ListNode(-1);dummy->next = head;// 寻找到反转区间ListNode* a = dummy;ListNode* b = dummy;for (int i = 0; i < right; i++) {if (i < left - 1) {a = a->next;}b = b->next;}ListNode* tailHead = b->next;// 反转区间ListNode* newHead = reverseAb(a->next, tailHead);// 区间前后链接a->next->next = tailHead;a->next = newHead;return dummy->next;}
};

234. 回文链表

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* reverse(ListNode* head) {ListNode* pre = nullptr;ListNode* cur = head;while (cur != nullptr) {ListNode* next = cur->next;cur->next = pre;pre = cur;cur = next; }return pre;}bool isPalindrome(ListNode* head) {if (head == nullptr) return true;ListNode* fast = head;ListNode* slow = head;while (fast != nullptr && fast->next != nullptr) {fast = fast->next->next;slow = slow->next;}if (fast != nullptr) {slow = slow->next;}ListNode* p1 = head;ListNode* p2 = reverse(slow);while (p2 != nullptr) {if (p1->val != p2->val) return false;p1 = p1->next;p2 = p2->next;}return true;}
};


http://www.ppmy.cn/server/28549.html

相关文章

Rust Web开发实战:构建高效稳定的服务端应用

如果你厌倦了缓慢、占用大量资源且不稳定的模板化Web开发工具&#xff0c;Rust就是你的解决方案。Rust服务提供了稳定的安全保证、非凡的开发经验&#xff0c;以及能够自动防止常见错误的编译器。 《Rust Web开发》教你使用Rust以及重要的Rust库(如异步运行时的Tokio、用于Web…

centos 7 yum install -y nagios

centos 7 systemctl disable firewalld --now vi /etc/selinux/config SELINUXdisabled yum install -y epel-release httpd nagios yum install -y httpd nagios systemctl enable httpd --now systemctl enable nagios --now 浏览器 IP/nagios 用户名&#xff1a;…

什么是HTTP?

什么是HTTP&#xff1f; HTTP基本概念HTTP 是什么&#xff1f;HTTP 常见的状态码有哪些&#xff1f;HTTP 常见字段有哪些&#xff1f; HTTP特性HTTP/1.1 的优点有哪些&#xff1f;HTTP/1.1 的缺点有哪些&#xff1f; HTTP基本概念 HTTP 是什么&#xff1f; HTTP 是超文本传输…

swin transformer 论文阅读

swin transformer 论文阅读 摘要1 Introduction结论2 Related Work3. Method3.1 整体流程3.2 移窗口自注意力3.2.1 窗口自注意3.2.2移动窗口自注意力3.2.3 其他 3.3 变体 4.Experiments 摘要 提出一个新的transformer&#xff0c;swin transfomer&#xff0c;可做为视觉领域一…

LeetCode LCR 179. 和为s的两个数字

原题链接&#xff1a;LCR 179. 查找总价格为目标值的两个商品 - 力扣&#xff08;LeetCode&#xff09; 题目的意思&#xff1a;通过给定的数组&#xff0c;找出两个值&#xff0c;相加并等于目标值。 第一种思路&#xff0c;暴力枚举&#xff0c;伪代码如下&#xff1a; for (…

【Canvas技法】流星雨的实现

【关键点】 流星的绘制&#xff0c;本质上还是绘制一条直线&#xff0c;但在渲染上有差别。 通常绘制直线都是给的固定颜色&#xff0c;绘制流星给的是渐变色&#xff0c;渐变色的开头是与背景色对比度明显的亮色&#xff0c;结尾是与背景色相同的暗色&#xff0c;中间渐变过…

信息管理与信息系统就业方向及前景分析

信息管理与信息系统(IMIS)专业的就业方向十分广泛&#xff0c;包含计算机方向、企业信息化管理、数据处理和数据分析等&#xff0c;随着大数据、云计算、人工智能、物联网等技术的兴起&#xff0c;对能够处理复杂信息系统的专业人才需求激增&#xff0c;信息管理与信息系统就业…

纯血鸿蒙APP实战开发——评论组件案例实现

介绍 评论组件在目前市面上的短视频app中是一种很常见的场景&#xff0c;本案例使用全局状态保留能力弹窗来实现评论组件。点击评论按钮弹出评论组件&#xff0c;点击空白处隐藏该组件&#xff0c;再次点击评论按钮则会恢复上一次浏览的组件状态。 效果图预览 使用说明 点击…