题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1)。

分析:

(1)利用快慢指针找到链表的中心

(2)进行步骤(1)的过程中,对前半部分链表进行反转

(3)如果链表长是偶数,首先比较slow和slow->next的值是否相等,若不相等返回false,否则,比较以slow -> next -> next开头的链表和以suf1开头的链表比较是否相等

(4)如果链表长是奇数,则将以slow -> next开头的链表和以suf1开头的链表比较是否相等

/**
* 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 == NULL) return true;
ListNode *fast = head;
ListNode *slow = head;
ListNode *suf1 = head -> next;
ListNode *suf2;
while(fast && fast -> next){
fast = fast -> next -> next;
suf2 = suf1 -> next;
suf1 -> next = slow;
slow = suf1;
suf1 = suf2;
}
head -> next = NULL;
if(fast){
slow = slow -> next;
}
else{
if(slow -> val != slow -> next -> val) return false;
slow = slow -> next -> next;
}
while(slow && suf1){
if(slow -> val != suf1 -> val) return false;
slow = slow -> next;
suf1 = suf1 -> next;
}
return true;
}
};

  

LeetCode 234. Palindrome Linked List(判断是否为回文链表)的更多相关文章

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

    重点是: 1.快慢指针找到链表的中点.快指针一次走两步,慢指针一次走一步,分清奇偶数情况. 2.反转链表.pre代表已经反转好的,每次将当前节点指向pre /* 快慢指针得到链表中间,然后用206题方 ...

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

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

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

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

  5. LeetCode 234 Palindrome Linked List(回文链表)(*)(?)

    翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...

  6. Leetcode 234 Palindrome Linked List 链表

    判断链表是否是回文. 我直接将链表的一半进行倒置,然后将两半的链表进行比较 /** * Definition for singly-linked list. * struct ListNode { * ...

  7. leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  8. LeetCode 234 Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...

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

随机推荐

  1. chrome headless+selenium+python+(ubuntu 16.04/centos7) 下的实现

    Ubuntu 16.04 下: 0x01 安装chrome 1 下载源加入系统源列表 sudo wget http://www.linuxidc.com/files/repo/google-chrom ...

  2. linux 网卡限速

    #安装git yum -y install git #下载wondershaper git clone  https://github.com/magnific0/wondershaper.git 第 ...

  3. ASP.NET/C# Razor视图引擎深入浅出

    在ASPX中我们使用 <% %>在里面编写C#代码在Razor中我们将会使用@{}编写C#代码1.基础——@+属性显示计算1+2的值:<span>1+2=@(1+2)</ ...

  4. 【visio】数据可视化 - 数据展示

    本章讲解如何将形状数据展示成数据图像,也就是将添加的属性,展示在图形上 1.数据图形控制面板 选中图形>右键>数据>编辑数据图形 2.新建数据图形 数据字段:也就是图形的属性 显示为 ...

  5. AcWing 861. 二分图的最大匹配 匈牙利算法

    #include <cstring> #include <iostream> #include <algorithm> using namespace std; , ...

  6. fiddler抓包手机准备工作

    1.勾选connections中的:Allow remote computers to connect 2.cmd中查找IP 3.在手机连接的WiFi中设置手动代理为:IP port:8888 4.在 ...

  7. Python 报错 AttributeError: module 'django.db.models' has no attribute 'SubfieldBase'

    AttributeError: module 'django.db.models' has no attribute 'SubfieldBase' http://www.guanggua.com/qu ...

  8. vscode git连接github

    上一篇文章中介绍了vscode中git的简单使用方法vscode git的简单使用 上次只讲到了本地库的创建,这次说明下怎么push到github上 首先需要有一个github的账号  github官 ...

  9. 谈谈一些有趣的CSS题目-- 单行居中,两行居左,超过两行省略

    开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...

  10. Linux下Nginx1.9.9的安装

    1.环境安装 yum install gcc-c++  .yum -y install pcre*.yum -y install openssl* (安装顺序安装) 2.下载压缩包(这里我使用的是老本 ...