题目

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?

分析

判断一个链表是否为回文。

要求时间复杂度O(n) 空间复杂度O(1)

若是可用辅助空间,则其是一个简单的题目,我们可用利用一个辅助数组存储节点元素val值,然后判断数组是否回文即可。

题目要求O(1)的空间复杂度,不能用辅助空间。我们只能从链表本身下手,既然判断回文,需要将链表一分为二,判断两部分是否对称,由于为单向链表,采取反转其中一半链表,然后逐个对比。

如何将链表一分为二呢?采用快行指针的方法,设置slow和fast,slow每行一步,fast行走两步,当fast走到链表末尾,slow则恰好走到中间。(注意处理链表长度为奇数的情况)

然后采用头插法将后半部分链表反转,再与前半部分对比。

AC代码

/**
* 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 || head->next == NULL)
return true; ListNode *fast = head, *slow = head;
while (fast && fast->next)
{
fast = fast->next->next;
slow = slow->next;
}
//若奇数个节点,跳过中间节点
if (fast != NULL)
fast = slow->next;
else
fast = slow;
slow = head; //头插法反转后半部分节点
ListNode *secHead = NULL;
while (fast)
{
ListNode *r = fast->next;
fast->next = secHead;
secHead = fast;
fast = r;
} //比较两部分
fast = secHead;
while (fast)
{
if (fast->val != slow->val)
{
return false;
}//if
fast = fast->next;
slow = slow->next;
}//while
return true;
}
};

GitHub测试程序源码

LeetCode(234) Palindrome Linked List的更多相关文章

  1. LeetCode(131)Palindrome Partitioning

    题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  2. LeetCode(206) Reverse Linked List

    题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...

  3. LeetCode(92) Reverse Linked List II

    题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1- ...

  4. LeetCode(9)Palindrome Number

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

  5. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

随机推荐

  1. 一起来学Spring Cloud | 第四章:服务消费者 ( Feign )

    上一章节,讲解了SpringCloud如何通过RestTemplate+Ribbon去负载均衡消费服务,本章主要讲述如何通过Feign去消费服务. 一.Feign 简介: Feign是一个便利的res ...

  2. 开源项目android-uitableview介绍

    在iOS应用中,UITableView应该是使用率最高的视图之一了.iPod.时钟.日历.备忘录.Mail.天气.照片.电话.短信. Safari.App Store.iTunes.Game Cent ...

  3. ABAP自定义功能函数

    1.实现计算器中阶乘函数 FUNCTION zfun_mm_001. *"---------------------------------------------------------- ...

  4. 零基础逆向工程22_PE结构06_导入表

    导入表结构 typedef struct _IMAGE_IMPORT_DESCRIPTOR { union { DWORD Characteristics; DWORD OriginalFirstTh ...

  5. 谷歌浏览器Chrome developer tool详细介绍

    http://www.cr173.com/html/19114_4.html 第 4 页 js调试源码控制面板 5 源码控制面板(js调试) Javascript的调试,基本上是通过源码控制面板和命令 ...

  6. HDU 1124 Factorial (阶乘后缀0)

    题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将n个十进制数进行质因数分解,观察的得到只有2*5才会出现10.那么n!应含有min(2个数,5个数)个后缀0,明显5的个数必定比2少,所 ...

  7. URAL 1057 Amount of Degrees (数位DP,入门)

    题意: 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的,B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足了要求:  17 = 24+2 ...

  8. Java 变量及基本数据类型

    1.Java变量 1.1 变量的概念 内存中开辟的一块存储空间,用于存放运算过程中需要用到的数据: 该区域有自己的名称(变量名)和类型(数据类型): 该区域的数据可以在同一类型范围内不断变化: 1) ...

  9. Android(java)学习笔记117:SharedPreferences(轻量级存储类)

    1.SharedPreferences是Android平台上一个轻量级的存储类,简单的说就是可以存储一些我们需要的变量信息.2个activity 之间的数据传递除了可以他通过intent来传递数据,还 ...

  10. 2018.4.12 各个系统安装MyEclipse过程(包括Mac、Linux、Windows)

    首先下载MyEclipse 最新官网在这里http://www.myeclipsecn.com/ mac 安装 . 在安装第一步会显示 "安装myeclipse显示更低版本javase6&q ...