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 ...
随机推荐
- Fleet-运行一个高可用的服务
运行一个高可用的服务 使用CoreOS最大的好处就是你可以以高可用的方式来运行你的服务.接下来我们将部署两个一样的Apache web server容器.然后,我们将通过让一台机器出现故障,fleet ...
- mysqldatadir 转移
当mysql data路径与原始目录不一致时 ,请在mysql 安装目录下my-default.ini 进行设置,取消对应#注释的地址,设置新地址,保存,重新启动,即可. 从网上各种搜索啊,各种尝试, ...
- pycharm使用秘籍 和 pip命令
python使用requirements.txt批量安装包 requirements.txt文件格式: requests==1.2.0 Flask==0.10.1 等等一系列包 cd 到requir ...
- [uestc oj]H - 邱老师选妹子
H - 邱老师选妹子 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- Android(java)学习笔记113:Activity的生命周期
1.首先来一张生命周期的总图: onCreate():创建Acitivity界面 onStart():让上面创建的界面可见 onResume():让上面创建的界面 ...
- [web开发] Vue + spring boot + echart 微博爬虫展示平台
1.微博登录 2.爬取数据 3.mysql存储 4.pyechart本地展示 5.用vue搭建网站web展示 先放图: 1.微博登录 新浪微博的登录不是简单的post就能解决的,他的登录有加密,所以我 ...
- 徒手教你使用zookeeper编写服务发现
zookeeper是一个强一致[不严格]的分布式数据库,由多个节点共同组成一个分布式集群,挂掉任意一个节点,数据库仍然可以正常工作,客户端无感知故障切换.客户端向任意一个节点写入数据,其它节点可以立即 ...
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...
- python之函数的初识
1. 面向过程编程的缺点 代码重复 代码可可读性不高 2. 函数的定义*** 函数是以功能为导向,一个函数封装一个功能.登录,注册,文件的改的操 3.函数的作用*** 函数减少代码的重复性,增 ...
- C++利用偏移量对文件操作
对输入流操作:seekg()与tellg()对输出流操作:seekp()与tellp()下面以输入流函数为例介绍用法: seekg()是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基 ...