【leetcode】234. Palindrome Linked List
/**
* 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 || !head->next) return true;
stack<int> prehalf;
ListNode *fast = head, *slow = head;
prehalf.push(head->val);
while(fast->next && fast->next->next)
{
slow = slow->next;
fast = fast->next->next;
prehalf.push(slow->val);
}
if(!fast->next) prehalf.pop();
while(slow->next)
{
slow = slow->next;
if(slow->val != prehalf.top()) return false;
prehalf.pop();
}
return true; }
};
要求使用O(1)的空间,那就是不能使用stack了,那么如何代替stack的作用呢,用stack的目的是为了利用其后进先出的特点,以便倒着取出前半段的元素。那么现在不用stack了,如何倒着取元素呢?可以在找到中点后,将后半段的链表翻转一下,这样就可以按照回文的顺序比较了。
solution:
/**
* 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 *slow=head, *fast=head;
while((fast->next) && (fast->next->next))
{
slow = slow->next;
fast = fast->next->next;
}
//reverse last.
ListNode *last = slow->next, *pre = head;
ListNode *reverse = NULL;
while(last)
{
ListNode *tmp = last;
last = tmp->next;
tmp->next = reverse;
reverse = tmp;
}
//compare pre and reverse.
while(reverse)
{
if(reverse->val != pre->val) return false;
pre = pre->next;
reverse = reverse->next;
}
return true; }
};
注意这个解决方法在翻转链表的过程中容易出错,一定要熟练掌握链表翻转的操作过程!!!
【leetcode】234. Palindrome Linked List的更多相关文章
- 【LeetCode】234. Palindrome Linked List (2 solutions)
Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【easy】234. Palindrome Linked List
ques: 判断一个链表是否回文 Could you do it in O(n) time and O(1) space? method:先将链表分为两部分,将后半部分反转,最后从前往后判断是否相等. ...
- 【leetcode❤python】 234. Palindrome Linked List
#-*- coding: UTF-8 -*-class Solution(object): def isPalindrome(self, head): ""&q ...
- 【leetcode】1278. Palindrome Partitioning III
题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【LeetCode】203. Remove Linked List Elements 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 递归 日期 题目地址:https://lee ...
随机推荐
- 【转】C# string数组转int数组
//字符串数组(源数组) string[] sNums = new[] {"1", "2"}; //整型数组(目标数组) int[] iNums; //转换方法 ...
- tls 流量画像——直接使用图像处理的思路探索,待进一步观察
代码,示意了一个tls的数据内容: import numpy as np import matplotlib.pyplot as pyplot # !!! If on the server, use ...
- js 时间戳转特定格式的日期
var Tools = {}; Tools.formatDate = function (fmt,timestamp) { if(timestamp){ var date = new Date(par ...
- openssh允许root用户登录
openssh默认是不允许root用户登录的,未改配置直接以正确的root账号密码登录会提示用户名密码错误之类. 好消息是openssh配置十分清晰,基本所有的服务端配置都在/etc/ssh/sshd ...
- 随机森林(Random Forest),决策树,bagging, boosting(Adaptive Boosting,GBDT)
http://www.cnblogs.com/maybe2030/p/4585705.html 阅读目录 1 什么是随机森林? 2 随机森林的特点 3 随机森林的相关基础知识 4 随机森林的生成 5 ...
- thinkphp 3.2 加载第三方库 第三方命名空间库
tp 自动加载的介绍: http://document.thinkphp.cn/manual_3_2.html#autoload 第三方库不规范库 不适用命名空间的库 可以使用import函数导入,其 ...
- Ubuntu Windows双系统重装windows后看不到ubuntu启动引导
1.下载并安装Easy BCD 2.点击编辑引导菜单,看到只有windows一项 3.点击“添加新条目”,添加引导菜单,选择linux/bsd ,类型选择GRUB 2,然后输入名称,选择Ubuntu所 ...
- linux系统管理 启停命令
mac下Linux的登录命名 'ssh -l root 192.168.10.109' password: xxxx 退出登录 >> logout shutdown命令 要使用这个命令 ...
- js 图片延时加载
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- 每天CSS学习之border-radius
css3的border-radius属性,我们用之来画圆角边框. 1.border-radius:none;//表示不用圆角边框,边框会变成方形. 2.border-radius:水平方向{1,4}[ ...