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

234. Palindrome Linked List

目录

一、题目描述

方法一、使用栈

方法二、将链表全部结点值复制到数组,再用双指针法

方法三、递归法逆序遍历链表

方法四、快慢指针+反转链表


一、题目描述

234. Palindrome Linked List

方法一、使用栈

需要遍历两次。时间复杂度O(n),空间复杂度O(n)。

/*** 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:bool isPalindrome(ListNode* head) {stack<int> S;ListNode* cur = head;while(cur){S.push(cur->val);cur = cur->next;}cur = head;while(cur){if(cur->val != S.top()){return false;}cur = cur->next;S.pop();}return true;}
};

方法二、将链表全部结点值复制到数组,再用双指针法

也是需要遍历两次,时间复杂度O(n)。空间复杂度O(n)。

/*** 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:bool isPalindrome(ListNode* head) {vector<int> nums;nums.reserve(100000);ListNode* cur = head;while(cur){nums.push_back(cur->val);cur = cur->next;}int left = 0;int right = nums.size()-1;while(left<right){if(nums[left]!=nums[right])return false;left++;right--;}return true;}
};

方法三、递归法逆序遍历链表

可以用递归法逆序遍历链表。用全局变量正序遍历链表。这样就实现了同时从两端遍历链表。

时间复杂度O(n)。递归栈的深度是n,所以空间复杂度还是O(n)。

/*** 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 {ListNode* forward = nullptr;//从链表头向链表尾正向遍历的指针
public:bool isPalindrome(ListNode* head) {forward = head;//题目已经保证head不为nullptr,至少有一个结点return recursiveCheck(head);}//用递归来逆序遍历链表bool recursiveCheck(ListNode* cur){if(cur->next){if(!recursiveCheck(cur->next))return false;}if(cur->val != forward->val)return false;forward = forward->next;return true;}
};

方法四、快慢指针+反转链表

将链表后半部分反转。然后同时遍历前半部分和后半部分,逐个比较。

第一步,用快慢指针法找到链表后半部分的开头结点。如果结点个数是奇数,则正中间的结点算作后半部分的开头也可以。参考leetcode 876. 链表的中间结点-CSDN博客

第二步,反转后半部分链表。参考leetcode 206. 反转链表-CSDN博客

第三步,同时遍历前半部分和后半部分,判断是否回文。

第四步,恢复原链表。

第五步,返回结果。

时间复杂度O(n)。空间复杂度O(1)。

/*** 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:bool isPalindrome(ListNode* head) {ListNode* slow = head;ListNode* fast = head;while(fast&&fast->next){fast = fast->next->next;slow = slow->next;}ListNode* middle = slow;//把链表后半部分反转ListNode* reveredRight = reverse(middle);ListNode* right = reveredRight;ListNode* left = head;bool res = true;while(left != middle){if(left->val != right->val){res = false;break;}left = left->next;right = right->next;}//恢复原链表reverse(reveredRight);return res;}ListNode* reverse(ListNode* head){ListNode* pre = nullptr;ListNode* cur = head;ListNode* nex = nullptr;while(cur){nex = cur->next;cur->next = pre;pre = cur;cur = nex;}return pre;}
};
http://www.xdnf.cn/news/9570.html

相关文章:

  • Linux系统编程-DAY07
  • JAVA中常用算法详解:排序(冒泡、快速排序)与查找(二分查找)
  • 途景VR智拍APP:开启沉浸式VR拍摄体验
  • 快速入门Java+Spring Ai+deepseek 开发
  • git 一台电脑一个git账户,对应多个仓库ssh
  • ParakeetTDT0.6BV2,语音识别ASR,极速转录, 高精度英文转录,标点支持(附整合包)
  • Dify案例实战之智能体应用构建(二)
  • IBM DB2和MYSQL在安全性、稳定性等方面的差异
  • 时间序列预测算法中的预测概率化笔记
  • GPIO驱动实例代码
  • 【客户案例】借助 DHTMLX Gantt 和 Diagram 构建高效项目与流程管理平台
  • 基于SpringBoot开发一个MCP Server
  • vue 中的ref属性
  • chown修改不成功的解决方案
  • ESP8285乐鑫SOCwifi芯片32bit MCU和2.4 GHz Wi-Fi
  • 零衍课堂 | 环境初始化部署流程
  • 从0到1:多医院陪诊小程序开发笔记(上)
  • VMware 安装 Ubuntu 实战教程
  • python学习打卡day38
  • 截图后怎么快速粘贴到notability?
  • day22-定时任务故障案例
  • 秒杀系统—2.第一版初步实现的技术文档
  • 医院闭环系统业务介绍
  • Linux基础 -- 设备树引脚复用之`/omit-if-no-ref/` 用法解析
  • 8.7 基于EAP-AKA的订阅转移
  • Springboot 集成 TDengine3.0版本
  • git stash 的使用
  • qt ubuntu 20.04 交叉编译
  • python实战:在Linux服务器上使用LibreOffice命令行批量接受Word文档的所有修订
  • MCP 与 AI 模型的用户隐私保护——如何让人工智能更懂“界限感”?