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 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;
}
};
LeetCode(234) Palindrome Linked List的更多相关文章
- LeetCode(131)Palindrome Partitioning
题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- LeetCode(206) Reverse Linked List
题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...
- 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- ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
随机推荐
- oracle中CAST函数使用简介【转】
CAST()函数可以进行数据类型的转换. CAST()函数的参数有两部分,源值和目标数据类型,中间用AS关键字分隔. 以下例子均通过本人测试. 一.转换列或值 语法:cast( 列名/值 as 数据类 ...
- JAVA_HOME not recognized by tomcat7 in Ubuntu
vi .bashrc 添加: export JAVA_HOME=/usr/lib/jvm/java--oracle export JRE_HOME=$JAVA_HOME/jre export CLAS ...
- 报错:java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xxx.entity.PersonEntity
报错:java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xxx.entity.PersonEntity 代 ...
- js字符串与十六进制之间的转换
在寻找加密解密的时候看到一个方法,代码图片转换.原理为:字符可以转为16进制,与图片RGB的一个R/G/B相对应,即一个像素点可容纳3个字符(注:Canvas的RGBA,透明度A似乎不能使用,使用后, ...
- ubuntu 12.04 source.list 源更新
官方源: #deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120423)]/ precise main restri ...
- 手机安全卫士——在设置中心 自定义view和自定义属性
自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载 ...
- npm相关命令
npm install npm install log4js npm list npm list log4js #查看模板安装版本 npm install log4js@1.0.1 #指定模块版本安装 ...
- Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...
- HttpRunner环境搭建
官方文档地址:http://cn.httprunner.org/官方源码地址:https://github.com/HttpRunner/HttpRunner HttpRunner 是一款面向 HTT ...
- HDU 5500 Reorder the Books (水题)
题意: 有n本书,编号为1~n,现在书的顺序乱了,要求恢复成有序的样子,每次只能抽出其中一本并插到最前面,问最少需要多少抽几次? 思路: 如果pos[i]放的不是书i的话,则书i的右边所有的书都必须抽 ...