题目

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

Subscribe to see which companies asked this question


思路

我的思路

想了半天没有想出空间为1的解法,就用了最先考虑到的数据结构栈来实现了。首先定义两个快慢指针,从头开始遍历,将慢指针压进栈内,当快指针走到尾时,慢指针指向的就确定了中间位置。然后慢指针继续前进,同时和栈顶元素进行比较,如果不相等则返回false。

Hot 解法

开始都是使用快慢指针确定了中间位置,然后它实现了一个翻转链表的操作,将慢指针还未走过的链表翻转了,然后对翻转的链表和从头开始的链表进行比较。


代码

我的代码

bool isPalindrome(ListNode* head) {if (!head) return true;stack<ListNode*> st;ListNode* fast=head;ListNode* slow=head;while (fast&&fast->next){st.push(slow);fast=fast->next->next;slow=slow->next;}if (fast) slow=slow->next;while (!st.empty() && slow->val==st.top()->val){st.pop();slow=slow->next;}return st.empty();}

Hot解法

bool isPalindrome(ListNode* head) {if(head==NULL||head->next==NULL)return true;ListNode* slow=head;ListNode* fast=head;while(fast->next!=NULL&&fast->next->next!=NULL){slow=slow->next;fast=fast->next->next;}slow->next=reverseList(slow->next);slow=slow->next;while(slow!=NULL){if(head->val!=slow->val)return false;head=head->next;slow=slow->next;}return true;}ListNode* reverseList(ListNode* head) {ListNode* pre=NULL;ListNode* next=NULL;while(head!=NULL){next=head->next;head->next=pre;pre=head;head=next;}return pre;}

链表翻转

链表翻转就是一道比较经典的题目,此处是一个O(1)的实现。最重要最需要注意的两个地方是提前保存下一个节点(next)和保存翻转后的头结点(prev)。

ListNode* reverseList(ListNode* head) {ListNode* pre=NULL;ListNode* next=NULL;while(head!=NULL){next=head->next;head->next=pre;pre=head;head=next;}return pre;}

记忆小技巧:在while循环里一共四句话,最开始当然是要提前保存正常顺序下的下一个节点,可以发现它们都是收尾相接的,最后移动head到保存好的next节点。

【LeetCode】234. Palindrome Linked List相关推荐

  1. 【easy】234. Palindrome Linked List

    ques: 判断一个链表是否回文 Could you do it in O(n) time and O(1) space? method:先将链表分为两部分,将后半部分反转,最后从前往后判断是否相等. ...

  2. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  3. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

  4. 【回文串7】LeetCode 234. Palindrome Linked List

    LeetCode 234. Palindrome Linked List Solution1:我的答案 一遍过,哈哈哈! /*** Definition for singly-linked list. ...

  5. 【Leetcode】Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 思路:若使用[Leetcode]Reverse I ...

  6. 【LeetCode】【HOT】234. 回文链表(存入数组)

    [LeetCode][HOT]234. 回文链表 文章目录 [LeetCode][HOT]234. 回文链表 package hot;import java.util.ArrayList; impor ...

  7. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  8. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  9. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

最新文章

  1. 你知道dos和cmd之间的关系以及区别吗?
  2. 如何去掉系统快捷方式的箭头(转载)
  3. jQuery循环使用相同类的元素
  4. (三)PHP网页架站
  5. 华硕笔记本,宽带连上,可以上网, 但收到不无线
  6. java反编译微信小程序_教你如何一键反编译获取任何微信小程序源代码(图形化界面,傻瓜式操作)...
  7. js之 foreach, map, every, some
  8. [vue-element] ElementUI表格组件如何实现动态表头?
  9. 在Visual Studio上开发Node.js程序(2)——远程调试及发布到Azure
  10. 负数的开方到底等于多少?
  11. openlayers5之热力图heatmap
  12. java计时器工作方法,java-摆动计时器如何工作?
  13. 第二十三章:准备原材料
  14. CLion IDE 来调试 JVM 源码
  15. 三轴传感器、六轴传感器、九轴传感器的文章解读
  16. Excel中输入整数却总是显示小数,如何调整?
  17. 台式计算机不同处理器型号,买电脑不要再被坑了,CPU型号解读
  18. 常见的棋牌游戏网站支付接口
  19. 免费的Kindle电子书资源
  20. 【Three.js入门】灯光与阴影、平行光阴影属性、聚光灯的属性和应用

热门文章

  1. Android--LayoutAnimation介绍
  2. 6-1 圆的面积 (10 分)
  3. JS,BOM,DOM(六)
  4. python实现的基于NMF的多图聚类算法
  5. 库存商品出入库方式分析收藏
  6. 计算机网络mtu值设置,Win7修改本地连接MTU值来提高网速的方法
  7. Python OpenCV相机参数:如何获取和修改相机参数
  8. Oilfield Big Bowl Decanter Centrifuge with VFD and PLC to Canada
  9. AI编程软件会取代程序员吗?
  10. geoserver wfs属性查询