题目

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. 应用的入口——Startup

    应用的入口——Startup 一个ASP.NET Core应用被启动之后就具有了针对请求的处理能力,而这个能力是由管道赋予的,所以应用的启动同时意味着管道的成功构建.由于管道是由注册的服务器和若干中间 ...

  2. SpringBoot---Web开发---Thymeleaf模板引擎

    一.前言 1.JSP在内嵌的Servlet容器中运行有一些问题: 1.1 内嵌的Tomcat.Jetty不支持以jar形式运行JSP: 2.2 Undertow不支持JSP: 2.SpringBoot ...

  3. c++笔记1

    using namespace std:命名空间可以保证一些命名能够在全局不冲突.如用户可以通过声明命名空间,然后用运算符::区别同名的不同变量 using namespace std;namespa ...

  4. mysql通过sql语句判断某个字段在一张表中是否存在

    应用场景: 我有一张表,表里面都是用户用来激活游戏的激活码,当用户在前端页面输入激活码时,要查询数据表中是否有这条激活码,如果有就返回"1",没有则返回"0". ...

  5. 常用的 HTML 头部标签

    曾几何时,我们已经不再手写 HTML 标签.Emmet.Markdown 等工具让我们「健步如飞」,但是我们真的了解这些标签了吗? 基本标签 使用 HTML5 doctype,不区分大小写. < ...

  6. 高效的设计可视化UI

    http://www.uimaker.com/uimakerdown/uitutorial/35990.html http://maqetta.org/downloads/ .Data.js Data ...

  7. Xiaocms 去版权

    Xiaocms 去版权 后台去版权: 1.  登录页面 修改文件:\admin\template\login.tpl.php 代码: <td width="190" rows ...

  8. OPEN SQL

    OPEN SQL 1.SELECT .INSERT.UPDATE.DELETE.MODIFYSELECT 命令包含如下从句:SELECT: 需要查询资料库指定表的那些列,是一行还是多行INTO: 查询 ...

  9. Android Framework中的Application Framework层介绍

    Android的四层架构相比大家都很清楚,老生常谈的说一下分别为:Linux2.6内核层,核心库层,应用框架层,应用层.我今天重点介绍一下应用框架层Framework,其实也是我自己的学习心得. Fr ...

  10. 用指针的方式实现,重写strrchr函数的功能

    char *strchrTest(char * ptr,char c); Action(){ char str[]={"thisisadog"}; char c='s'; lr_o ...