题目

请编写一个函数,检查链表是否为回文。

给定一个链表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:回文链表的更多相关文章

  1. [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 ...

  2. [CareerCup] 2.7 Palindrome Linked List 回文链表

    2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...

  3. lintcode 中等题:Palindrome Linked List 回文链表

    题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...

  4. [Swift]LeetCode234. 回文链表 | Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  5. Leetcode:234 回文链表

    leetcode:234 回文链表 关键点:请判断一个链表是否为回文链表.示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true. ...

  6. 判断回文字符串、回文链表、回文数(python实现)

    所谓回文字符串,就是正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.即是对称结构 判断回文字符串 方法一: def is_palin ...

  7. 如何判断一个单向链表是否为回文链表(Palindrome Linked List)

    题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案 ...

  8. 【leetcode 简单】 第六十七题 回文链表

    请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O(n) 时间复 ...

  9. 回文链表 · Palindrome Linked List

    [抄题]: 设计一种方式检查一个链表是否为回文链表.1->2->1 就是一个回文链表. [暴力解法]: 时间分析: 空间分析: [思维问题]: 以为要从从后往前扫描,不知道调用revers ...

随机推荐

  1. c语言学习的第11天 指针

    #include<stdio.h> int main(void) { int * p; int i=3; int j; p=&i; j=*p; printf("i=%d, ...

  2. 搭建LoadRunner中的场景(一) 创建场景

    一.创建场景 1. 使用场景创建设置对话框 场景分类: 1. 人工场景:相比面向目标场景,人工场景在实际工作中的应用更为广泛. 2. 面向目标场景:预先定义了一个测试目标,LoadRunner将根据这 ...

  3. Linux_学习_02_ 重启tomcat与查看tomcat日志

    一.重启tomcat服务器 cd /home/ehlhec/tomcat_dingtalk/bin ./shutdown.sh ps -ef|grep java ./startup.sh (1) 进入 ...

  4. linux命令学习笔记(24):Linux文件类型与扩展名

    Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序 而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux ...

  5. POJ3107Godfather(求树的重心裸题)

    Last years Chicago was full of gangster fights and strange murders. The chief of the police got real ...

  6. BZOJ3065:带插入区间K小值

    浅谈树状数组与主席树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  7. C#编译问题'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument

    &apos;System.Collections.Generic.IEnumerable<string>&apos; does not contain a definiti ...

  8. windows下VisualStudio和QtCreator搭建Qt开发环境

    一.简介 集成开发平台IDE都有各自的长处,编写Qt程序可根据自己的喜好来选择相应的IDE.下述文章都是装载博友的文章,其中有很多细节还得自己调整. 二.详解 1.VisualStudio搭建Qt开发 ...

  9. eclipse 上svn插件的安装,百度知道

    打开eclipse -> Help ->Install New Software选项, 点击Add按钮   根据需要,添加自己需要的版本svn控制器的版本,填写name和url,点击ok. ...

  10. 算法导论笔记——第二十章 van Emde Boas树

    当关键字是有界范围内的整数时,能够规避Ω(lglgn)下界的限制,那么在类似的场景下,我们应弄清楚o(lgn)时间内是否可以完成优先队列的每个操作.在本章中,我们将看到:van Emde Boas树支 ...