题目

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. k8s的nfs存储外挂设置过程

    需求: 在k8s集群里的某个模块生成的目录文件或者更新的目录文件,存储到外面某台服务器上 1.安装nfs服务(192.168.1.2  Ubuntu 16.04) apt-get install nf ...

  2. 078 Subsets 子集

    给定一组不同的整数 nums,返回所有可能的子集(幂集).注意事项:该解决方案集不能包含重复的子集.例如,如果 nums = [1,2,3],结果为以下答案:[  [3],  [1],  [2],  ...

  3. D. Statistics of Recompressing Videos

    D. Statistics of Recompressing Videos time limit per test 3 seconds memory limit per test 256 megaby ...

  4. C#数据类型 值传递和引用传递

    /// <summary> /// 电脑类 /// </summary> public class Computer { public string Type { get; s ...

  5. css继承性

    不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...

  6. canvas、svg、canvas与svg的区别

    一.canvas canvas 画布,位图 <canvas> 标签定义图形,比如图表和其他图像,您必须使用脚本来绘制图形 注意:不要在style中给canvas设置宽高,会有位移差 can ...

  7. Arduino ESP8266编程深入要点

    Arduino for ESP8266的话,如果不修改代码,默认没有办法进入轻睡眠的省电模式,只能进入Modem Sleep,也就是说Wifi可以暂时睡眠但是CPU没法睡,Modem Sleep最低功 ...

  8. Android 调节图片工具类

    package com.base.changeimage; import android.graphics.Bitmap; import android.graphics.Canvas; import ...

  9. mysql 5.7.20 在线安装与卸载(yum卸载与rpm卸载方式)

    mysql5.7.20和之前的5.7.16版本不同,解压后没有data文件,需要自己建立 1.把下载的mysql5.7.20放到目录:/usr/local/2.卸载cenos上预装的mysql查看已安 ...

  10. 一键部署joomla开源内容管理平台

    https://market.azure.cn/Vhd/Show?vhdId=10896&version=12949 产品详情 产品介绍Joomla是一套自由.开放源代码的内容管理系统,以PH ...