【LeetCode】234. Palindrome Linked List
题目
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相关推荐
- 【easy】234. Palindrome Linked List
ques: 判断一个链表是否回文 Could you do it in O(n) time and O(1) space? method:先将链表分为两部分,将后半部分反转,最后从前往后判断是否相等. ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
- 【leetcode】解题日记(未完待续)
开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...
- 【回文串7】LeetCode 234. Palindrome Linked List
LeetCode 234. Palindrome Linked List Solution1:我的答案 一遍过,哈哈哈! /*** Definition for singly-linked list. ...
- 【Leetcode】Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 思路:若使用[Leetcode]Reverse I ...
- 【LeetCode】【HOT】234. 回文链表(存入数组)
[LeetCode][HOT]234. 回文链表 文章目录 [LeetCode][HOT]234. 回文链表 package hot;import java.util.ArrayList; impor ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【leetcode】86. Partition List
题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
最新文章
- 你知道dos和cmd之间的关系以及区别吗?
- 如何去掉系统快捷方式的箭头(转载)
- jQuery循环使用相同类的元素
- (三)PHP网页架站
- 华硕笔记本,宽带连上,可以上网, 但收到不无线
- java反编译微信小程序_教你如何一键反编译获取任何微信小程序源代码(图形化界面,傻瓜式操作)...
- js之 foreach, map, every, some
- [vue-element] ElementUI表格组件如何实现动态表头?
- 在Visual Studio上开发Node.js程序(2)——远程调试及发布到Azure
- 负数的开方到底等于多少?
- openlayers5之热力图heatmap
- java计时器工作方法,java-摆动计时器如何工作?
- 第二十三章:准备原材料
- CLion IDE 来调试 JVM 源码
- 三轴传感器、六轴传感器、九轴传感器的文章解读
- Excel中输入整数却总是显示小数,如何调整?
- 台式计算机不同处理器型号,买电脑不要再被坑了,CPU型号解读
- 常见的棋牌游戏网站支付接口
- 免费的Kindle电子书资源
- 【Three.js入门】灯光与阴影、平行光阴影属性、聚光灯的属性和应用