234 Palindrome Linked List 回文链表
请检查一个链表是否为回文链表。
进阶:
你能在 O(n) 的时间和 O(1) 的额外空间中做到吗?
详见:https://leetcode.com/problems/palindrome-linked-list/description/
Java实现:
方法一:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
//0个节点或是1个节点
if(head==null||head!=null&&head.next==null){
return true;
}
ListNode slow=head;
ListNode fast=head;
Stack<Integer> stk=new Stack<Integer>();
stk.push(head.val);
while(fast.next!=null&&fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
stk.push(slow.val);
}
//链表长度为偶数
if(fast.next!=null){
slow=slow.next;
}
while(slow!=null){
int tmp=stk.pop();
if(slow.val!=tmp){
return false;
}
slow=slow.next;
}
return true;
}
}
方法二:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
//0个节点或是1个节点
if(head==null||head!=null&&head.next==null){
return true;
}
ListNode slow=head;
ListNode fast=head;
ListNode first=null;
while(fast!=null&&fast.next!=null){
first=slow;
slow=slow.next;
fast=fast.next.next;
}
fast=first.next;
first.next=null;
ListNode pre=null;
ListNode next=null;
while(fast!=null){
next=fast.next;
fast.next=pre;
pre=fast;
fast=next;
}
first=head;
fast=pre;
while(first!=null&&fast!=null){
if(first.val!=fast.val){
return false;
}
first=first.next;
fast=fast.next;
}
return true;
}
}
C++实现:
方法一:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head==nullptr||head->next==nullptr)
{
return true;
}
ListNode *slow=head;
ListNode *fast=head;
stack<int> stk;
stk.push(head->val);
while(fast->next&&fast->next->next)
{
slow=slow->next;
fast=fast->next->next;
stk.push(slow->val);
}
if(!fast->next)
{
stk.pop();
}
while(slow->next)
{
slow=slow->next;
int tmp=stk.top();
if(slow->val!=tmp)
{
return false;
}
stk.pop();
}
return true;
}
};
方法二:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(!head||!head->next)
{
return true;
}
ListNode *slow=head;
ListNode *fast=head;
ListNode *first=nullptr;
while(fast&&fast->next)
{
first=slow;
slow=slow->next;
fast=fast->next->next;
}
fast=first->next;
first->next=nullptr;
ListNode *pre=nullptr;
ListNode *next=nullptr;
while(fast)
{
next=fast->next;
fast->next=pre;
pre=fast;
fast=next;
}
first=head,fast=pre;
while(first&&fast)
{
if(first->val!=fast->val)
{
return false;
}
first=first->next;
fast=fast->next;
}
return true;
}
};
参考:https://www.cnblogs.com/grandyang/p/4635425.html
234 Palindrome Linked List 回文链表的更多相关文章
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- lintcode 中等题:Palindrome Linked List 回文链表
题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- LeetCode 234. Palindrome Linked List(判断是否为回文链表)
题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1). 分析: (1)利用快慢指针找到链表的中心 (2)进行步骤(1)的过程中,对前半部分链表进行反转 (3)如果链表长是偶数,首先比较 ...
- [Swift]LeetCode234. 回文链表 | Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- LeetCode OJ:Palindrome Linked List(回文链表判断)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- 如何判断一个单向链表是否为回文链表(Palindrome Linked List)
题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案 ...
随机推荐
- ELK pipeline
https://www.felayman.com/articles/2017/11/24/1511527532643.html?utm_medium=hao.caibaojian.com&ut ...
- [bzoj1014][JSOI2008]火星人prefix_非旋转Treap_hash_二分
火星人prefix bzoj-1014 JSOI-2004 题目大意:给定一个字符串,支持三种操作:1.查询:两个后缀之间的$LCP$:2.单点修改:3.插入一个字符. 注释:$1\le n\le 1 ...
- POJ2586 Y2K Accounting Bug 解题报告
Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vi ...
- POJ——T 2728 Desert King
http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27191 ...
- Servlet处理日期
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/handling-date.html: 使用Servlet的最重要的优势之一是可以使用核心Java ...
- Mybatis 增强工具包 Mybatis-Plus
原文:https://www.oschina.net/p/mybatis-plus
- Delphi 2007 的重构功能
Move 移动 1.将选定的静态函数从一个类移动到另一个类 2.将选中的类或接口移动到其他单元 Extract Interface 抽取接口 将选定的函数抽取到一个新的接口中 Extract Supe ...
- C#.NET如何判断是否有缺少的using
调试的时候会报错,红色的波浪线表示出错的位置,右击即可找到对应的using
- Python输入输出及其他
print用法 print会输出一个\n,也就是换行符,这样光标移动到了下一行行首,接着输出,之前已经通过stdout输出的东西依旧保留,而且保证我们在下面看到最新的输出结果.回车 \r 本义是光标重 ...
- ShadowDOM
HTML5 ShadowDOM & CustomElements KeKeMars 关注 2015.12.09 15:20* 字数 1239 阅读 1626评论 2喜欢 2 Web组件由四部分 ...