当前位置: 首页 > news >正文

【反转链表专题】【LeetCode206.反转链表】【LeetCode25.K个一组翻转链表】【LeetCode234.回文链表】

题目链接

  • 反转链表:206. 反转链表 - 力扣(LeetCode)
  • K个一组翻转链表:25. K 个一组翻转链表 - 力扣(LeetCode)
  • 回文链表:234. 回文链表 - 力扣(LeetCode)

实现思路

代码实现

  • 反转链表:
/*** 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) {ListNode* pre = nullptr;ListNode* cur = head;while(cur) {ListNode* next = cur -> next;cur -> next = pre;pre = cur;cur = next;}return pre;}
};
  • 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:vector<ListNode*> reverse(ListNode* head, ListNode* next) {ListNode* pre = next;ListNode* cur = head;while (cur != next) {ListNode* nextNode = cur -> next;cur -> next = pre;pre = cur;cur = nextNode;}return {pre, head}; // pre是反转之后的头节点,head是反转之后的尾节点}ListNode* reverseKGroup(ListNode* head, int k) {int cnt = 0;ListNode* cur = head;ListNode* preHead = new ListNode(-1);preHead -> next = head;ListNode* pre = preHead;while (cur) {cnt++;if (cnt % k == 0) {cnt = 0;vector<ListNode*> res = reverse(pre->next, cur->next);pre->next = res[0];cur = res[1];pre = cur;}cur = cur -> next;}return preHead -> next;}
};
  • 回文链表
/*** 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) {ListNode* next = cur -> next;cur -> next = pre;pre = cur;cur = next;}return pre;}bool isPalindrome(ListNode* head) {if (head -> next == nullptr) return true;ListNode* preHead = new ListNode();preHead -> next = head;ListNode* slow = preHead;ListNode* fast = preHead;while (fast != nullptr && fast -> next != nullptr) {slow = slow -> next;fast = fast -> next -> next;}// 反转slow->next到tail的部分ListNode* head1 = slow -> next;head1 = reverse(head1);ListNode* cur = head;ListNode* cur1 = head1;while (cur1) {if (cur -> val != cur1 -> val) {return false;}cur = cur -> next;cur1 = cur1 -> next;}return true;}
};

http://www.dtcms.com/a/279377.html

相关文章:

  • Spring Boot 自带的 JavaMail 集成
  • android Perfetto cpu分析教程及案例
  • 5G 到 6G通信技术的革新在哪里?
  • 腾讯云和火山云优劣势对比
  • 电力协议处理框架C++版(三)
  • CA-IS3082W 隔离485 收发器芯片可能存在硬件BUG
  • LTspic下载,帮助及演示电路
  • sfe_py的应力云图计算与显示step by step
  • 暑期自学嵌入式——Day02(C语言阶段)
  • 揭开图像的秘密:OpenCV直方图入门详解
  • 代数基本定理最简短的证明
  • 对于独热编码余弦相似度结果为0和词向量解决了词之间相似性问题的理解
  • ubuntu之坑(十五)——设备树
  • gRPC和http长轮询
  • 新手向:Python自动化办公批量重命名与整理文件系统
  • Dubbo 学习笔记
  • 谷歌收获成果:OpenAI收购Windsurf计划告吹
  • 工业软件加密锁复制:一场技术与安全的博弈
  • Mybatis05-参数和返回
  • 以太网供电(PoE)电源
  • 编程语言设计目的与侧重点全解析(主流语言深度总结)
  • vue中使用西瓜播放器xgplayer (封装)+xgplayer-hls 播放.m3u8格式视频
  • Spark 单机模式安装与测试全攻略​
  • STM32小实验1--点亮LED
  • # 电脑待机后出现死机不能唤醒怎么解决?
  • 【终极指南】ChatGPT/BERT/DeepSeek分词全解析:从理论到中文实战
  • 2025年人工智能与网络安全国际会议(IACAINS 2025)
  • vim扩展
  • Python Web框架对比:Flask vs FastAPI
  • Kubernetes控制器详解