请检查一个链表是否为回文链表。

进阶:
你能在 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 回文链表的更多相关文章

  1. [LeetCode] 234. Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  2. [CareerCup] 2.7 Palindrome Linked List 回文链表

    2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...

  3. [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 ...

  4. lintcode 中等题:Palindrome Linked List 回文链表

    题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...

  5. LeetCode 234:回文链表 Palindrome Linked List

    ​ 请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...

  6. LeetCode 234. Palindrome Linked List(判断是否为回文链表)

    题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1). 分析: (1)利用快慢指针找到链表的中心 (2)进行步骤(1)的过程中,对前半部分链表进行反转 (3)如果链表长是偶数,首先比较 ...

  7. [Swift]LeetCode234. 回文链表 | Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  8. 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 ...

  9. 如何判断一个单向链表是否为回文链表(Palindrome Linked List)

    题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案 ...

随机推荐

  1. 创建Django项目(一)

    2013-07-24 23:20:58|   最近在学习Django项目的创建,主要的参考资料是:Djangobook 和 Django Project.这些日志用来记录自己的学习过程吧.       ...

  2. codevs——1742 爬楼梯

    1742 爬楼梯  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 小明家外面有一个长长的楼梯,共N阶.小明的 ...

  3. MongoDB学习day03--索引和explain分析查询速度

    一.索引基础 db.user.ensureIndex({"username":1}) 创建索引,username为key,数字 1 表示 username 键的索引按升序存储, - ...

  4. php-fpm回顾和总结

    时间久了很容易忘,这里做个备份 FastCGI协议php语言的实现,可以高效处理来自web端的动态请求 php-fpm维护一个或者多个php-cgi进程池,处理请求时不需要频繁创建进程 所以比传统的C ...

  5. Bug记载2之Vue.JS路由定义的位置

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  6. 获取路由事件的源Source和OriginalSource

    路由事件的消息包括在RoutedEventArgs实例中,该实例有两个属性Source和OriginalSource,都是表示路由事件传递的起点.即事件消息的源头.仅仅只是Source表示的是Logi ...

  7. io口的作用

    I/O接口的作用     主机与外界交换信息称为输入/输出(I/O).主机与外界的信息交换是通过输入/输出设备进行的.一般的输入/输出设备都是机械的或机电相结合的产物,比方常规的外设有键盘.显示器.打 ...

  8. Android自己定义组件系列【5】——进阶实践(2)

    上一篇<Android自己定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这 ...

  9. Unix - 文件里构成一个空洞的分析

    lseek函数显示地为一个打开文件设置偏移量,文件偏移量能够大于文件的当前长度,在这样的情况下.对该文件的下一次写将加长该文件.并在文件里构成一个空洞,这一点是同意的. 位于文件里但没有写过的字节都被 ...

  10. Ubuntu安装JDK及环境变量配置(sun java)

    捣鼓了尽一天的时间,终于把sun的java安装上了,不是openjava了,网上试了好多的方法好多都是不可以的,所以当自己成功后就立马把方法贴出来,以方便后来者少走弯路,此文的方法绝对可行! 这里先简 ...