CC13:回文链表
题目
请编写一个函数,检查链表是否为回文。
给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。
测试样例:
{1,2,3,2,1}
返回:true
{1,2,3,2,3}
返回:false
解法
想法是这样,找到链表的中点,从中点之后的链表数据用栈进行存储;然后从链表的头部开始依次遍历直到中点处,每一次遍历都与栈的top进行比较,如果不相等就证明不是回文链表。这样的理解其实很简单,就是链表的后半部分通过栈编程反向的,从栈里依次取出的数据与链表首部到中点数据的顺序是一样的话,就说明是回文链表。代码如下:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class Palindrome {
public:
bool isPalindrome(ListNode* pHead) {
// write code here
if(pHead==NULL)
return true;
ListNode *pFast=pHead;
ListNode *pSlow=pHead;
ListNode *temp=pHead;
int cnt=0;
while(pFast->next!=NULL && pFast->next->next!=NULL)
{
pSlow=pSlow->next;
pFast=pFast->next->next;
}
while(pSlow->next!=NULL)
{
res.push(pSlow->val);
cnt++;
}
for(int i=0;i<cnt;i++)
{
int t=res.top();
if(temp->val!=t)
return false;
temp=temp->next;
res.pop();
}
return true;
}
private:
stack<int> res;
};
但是报错显示超过限制的内存,证明应该不能用栈这个方式进行存储,可能需要对链表进行逆序.所以将利用栈的方法稍加修改改成对链表进行逆序,果然OK了,代码如下:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class Palindrome {
public:
ListNode *reverse(ListNode *head) {
if(head == NULL)
return NULL;
ListNode *Next = head->next;
head->next = NULL;
while(Next != NULL)
{
ListNode *tmp = Next->next;
Next->next = head;
head = Next;
Next = tmp;
}
return head;
}
bool isPalindrome(ListNode* pHead) {
// write code here
if(pHead==NULL)
return true;
ListNode *pFast=pHead;
ListNode *pSlow=pHead;
ListNode *temp=pHead;
while(pFast->next!=NULL && pFast->next->next!=NULL)
{
pSlow=pSlow->next;
pFast=pFast->next->next;
}
pSlow->next=reverse(pSlow->next);
while(pSlow->next!=NULL)
{
pSlow=pSlow->next;
if(temp->val!=pSlow->val)
return false;
temp=temp->next;
}
return true;
}
};
这里值得注意一下的是逆序操作链表Reverse这个操作,看一下他函数是具体的操作:

CC13:回文链表的更多相关文章
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- lintcode 中等题:Palindrome Linked List 回文链表
题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...
- [Swift]LeetCode234. 回文链表 | Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- Leetcode:234 回文链表
leetcode:234 回文链表 关键点:请判断一个链表是否为回文链表.示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true. ...
- 判断回文字符串、回文链表、回文数(python实现)
所谓回文字符串,就是正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.即是对称结构 判断回文字符串 方法一: def is_palin ...
- 如何判断一个单向链表是否为回文链表(Palindrome Linked List)
题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案 ...
- 【leetcode 简单】 第六十七题 回文链表
请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O(n) 时间复 ...
- 回文链表 · Palindrome Linked List
[抄题]: 设计一种方式检查一个链表是否为回文链表.1->2->1 就是一个回文链表. [暴力解法]: 时间分析: 空间分析: [思维问题]: 以为要从从后往前扫描,不知道调用revers ...
随机推荐
- javascript正则(带g符号) 多次调用test 结果交替出现
链接:https://segmentfault.com/q/1010000000582051 http://stackoverflow.com/questions/2851308/why-does-m ...
- 微信video和audio无法自动播放解决方案
//音频,写法一<audio src="music/bg.mp3" autoplay loop controls>你的浏览器还不支持哦</audio> // ...
- listen 67
Pay What You Want May Deter Consumers Music, film and video game makers face a new online, digital w ...
- ICE 迁移64位安装问题
昨天手贱,在apt-get install 后有一大堆,上百个安装包not upgrade, 发现有提示apt-get autoremove,犹豫了很久后还是忍不住执行了autoremove:这个命令 ...
- 集训Day11
别人的题公开原题面不好 写题解吧 T1 很明显答案满足二分,二分之后算出每个人的位置,做一个LIS即可 T2 阅读体验极差 T3 根本不会 交都没交 期望得分200
- BZOJ3127:[USACO2013OPEN]Yin and Yang
浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...
- 再学IHanlder 类----------------关于Asp.net与iis原理网上看博客收获写一个验证码用一般处理程序记的好长时间前就写过不过现在再看有点不一样的感觉
建一个web网站 新建一般处理程序直接贴代码: using System;using System.Collections.Generic;using System.Linq;using System ...
- css属性学习
CSS display 属性 display 属性规定元素应该生成的框的类型. none:此元素不会被显示. block:此元素将显示为块级元素,此元素前后会带有换行符. inline默认.此元素会被 ...
- file -i haha.csv
user@user-desk ~/Downloads/largetrd$ file -i LT_Largetrd.csvLT_Largetrd.csv: text/plain; charset=utf ...
- 序列联配(alignment)和数据库搜索方法简介
根据一个打分系统,怎么样排对起来打分能够最大.就认为历史上应该是这样子的. 数据同源搜索软件Fasta和Blast 是目前功能最全,使用最广的同源性数据库搜索软件包.他们在Needleman的动态算法 ...