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

leetcode_234 回文链表

1. 题意

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。

2. 题解

2.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) {vector<int> a{};while ( head ) {a.push_back( head->val );head = head->next;}int l = 0;int r = static_cast<int>(a.size()) - 1;while ( l < r) {if ( a[l] != a[r] )return false;l++;r--;}return true;}
};
2.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:bool isPalindrome(ListNode* head) {stack<int> st;auto cur = head;for ( ;cur;cur=cur->next) {st.push(cur->val);}while (head) {if (head->val != st.top())return false;st.pop();head = head->next;}return true;}
};
2.3 尾递归+变量标记

其实这种方法跟栈的做法本质是一样的。

只是变得更加不好理解了。。。

你让我下次写,我不一定能写出来。

/*** 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 get_ans(ListNode *head, ListNode * &pre) {if ( head != nullptr ) {bool res = get_ans(head->next, pre);if ( res == false )return false;if ( pre->val != head->val)return false;pre = pre->next;}return true;}bool isPalindrome(ListNode* head) {ListNode *&lp = head;return get_ans( head, lp);}
};
2.4 反转后半部分链表比较

这种做法虽然复杂,但是很有价值。

找到链表的中间位置是leetcode-876. 链表的中间结点。

反转后半部分链表是leetcod-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 *get_mid(ListNode *head) {if (head == nullptr)return nullptr;auto slow = head;auto fast = head->next;while (fast && fast->next) {slow = slow->next;fast = fast->next->next;}return slow;}ListNode *reverse_list(ListNode *head) {ListNode *nHead{};auto cur = head;while ( cur ) { auto nxt = cur->next;cur->next = nHead;nHead     = cur;cur = nxt;}return nHead;}// void dump_list(ListNode *head) {//     while ( head ) {//         cout << head->val << " ";//         head = head->next;//     }//     cout << "\n";// }bool isPalindrome(ListNode* head) {auto pre_r = get_mid( head );auto nHead = reverse_list( pre_r->next );auto lp = head;auto rp = nHead;bool ans{ true };while ( rp ) {if ( lp->val != rp->val ) {ans = false;break;}lp = lp -> next;rp = rp -> next;}reverse_list( nHead );// dump_list( head );return ans;}
};

当然你要非要写左半部分遍历作为条件也不是不可以,

需要额外处理一些东西,包括左右部分链表的断链与恢复,

还有奇数情况的特殊处理,如何判断节点数是奇数呢?

fast指针值为空。

/*** 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 *nHead = nullptr;auto cur = head;while ( cur ) {auto nxt  = cur->next;cur->next = nHead;nHead     = cur;cur = nxt;}return nHead;}void dump_list(const ListNode *head){const ListNode *cur = head;while (cur != nullptr) {cout << cur->val << " ";cur = cur->next;}cout << "\n";}bool isPalindrome(ListNode* head) {if ( head == nullptr || head->next == nullptr )return true;auto slow = head;auto fast = head->next;while ( fast && fast->next ) {slow = slow->next;fast = fast->next->next;}auto lp = head;auto nrHead = reverseList( slow->next );auto rp = nrHead;slow->next = nullptr;auto lend = slow; if ( fast != nullptr )lend = nullptr;bool res{true};while ( lp != lend ) {if ( lp->val != rp->val ) {res = false;break;}lp = lp->next;rp = rp->next;}slow->next = reverseList( nrHead );return res;}
};

3. 参考

leetcode
0x3f


文章转载自:

http://NU2AHF9n.yLzdx.cn
http://ejsf68mX.yLzdx.cn
http://kkdcfduz.yLzdx.cn
http://vuyOJ0IP.yLzdx.cn
http://0N0F3YaA.yLzdx.cn
http://1wLgK8Cu.yLzdx.cn
http://BH1LDw5W.yLzdx.cn
http://fqhs10r2.yLzdx.cn
http://hNg2CyJE.yLzdx.cn
http://87f4znQL.yLzdx.cn
http://X8sQEYur.yLzdx.cn
http://1wrq3fm4.yLzdx.cn
http://HR9jD8wj.yLzdx.cn
http://plGWiBB1.yLzdx.cn
http://hQ4VUuV2.yLzdx.cn
http://wXYMFiCF.yLzdx.cn
http://5CyfTMt4.yLzdx.cn
http://qk8hSqP9.yLzdx.cn
http://d6JFbmZj.yLzdx.cn
http://YHiEDGOO.yLzdx.cn
http://9W7lvooE.yLzdx.cn
http://SuyZhI5W.yLzdx.cn
http://Hw20n5it.yLzdx.cn
http://DpLWs7l1.yLzdx.cn
http://6xr7e58W.yLzdx.cn
http://a0iptFYJ.yLzdx.cn
http://uorZsh6I.yLzdx.cn
http://cjagdEg1.yLzdx.cn
http://sgTkjtk2.yLzdx.cn
http://ZRkcRz7p.yLzdx.cn
http://www.dtcms.com/a/364854.html

相关文章:

  • 如何画时序图、流程图
  • try-catch:异常处理的最佳实践与陷阱规避
  • 2025年互联网行业专业认证发展路径分析
  • RoPE频率缩放机制:解密大语言模型上下文扩展的核心算法
  • 无人机散热模块技术要点分析
  • Diamond基础3:在线逻辑分析仪Reveal的使用
  • 超越马力欧:如何为经典2D平台游戏注入全新灵魂
  • 【Spring Cloud微服务】10.王子、巨龙与Spring Cloud:用注解重塑微服务王国
  • Maven动态控制版本号秘籍:高效发包部署,版本管理不再头疼!
  • .vsdx文件转pdf、word、ppt等文件在线分享(免费版)
  • 【MATLAB代码】UKF(无迹卡尔曼滤波)的组合导航,状态量为平面8维,观测量为XY坐标。附完整代码,有中文注释
  • Unity 的游戏循环机制
  • Vue基础知识-重要的内置关系:vc实例.__proto__.__proto__ === Vue.prototype
  • ESP32嵌入固件读取
  • AI大模型对决:谁是最强智能?
  • MySQL 8.0.40 主从复制完整实验总结(基础搭建 + 进阶延时同步与误操作恢复)
  • [信号与系统个人笔记]第三章 连续时间信号与系统的频域分析 Part 2
  • flutter 中间组件自适应宽度
  • 从在线工具到代码库:图表设计工具挑选指南
  • uniapp 开发上架 iOS App全流程
  • Spring Boot 事务失效的八大原因及解决方案详解
  • iOS 上架 uni-app 流程全解析,从打包到发布的完整实践
  • Hostol Magento电商服务器套餐:基于阿里云,预配置高性能环境,一键开店
  • CouponHub项目开发记录-基于责任链来进行创建优惠券模板的参数验证
  • Vue+Echarts饼图深度美化指南:打造卓越数据可视化体验
  • 【串口助手】串口调试助手LTSerialTool v3.12.0发布
  • 打靶日记-SQLi-LABS(二)
  • LeetCode 3132.找出与数组相加的整数2
  • 金融行业数智化转型:如何用企业微信AI实现高效内部协作与外部服务?
  • MCP(Model Context Protocol)介绍