检查链表是否为回文

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

#include<iostream>

#include<stack>

using namespace std;

typedef struct node

{

int data;

struct node* next;

}* LinkedListNode;

bool isPalindrome(LinkedListNode head)

{

LinkedListNode fast = head;

LinkedListNode slow = head;

stack<int> stack;

/*将链表前半部分元素入栈,当快速runner(

移动速度为慢速runner的两倍)到达链表尾部时,则

慢速runner已经处在链表中间位置*/

while (fast != NULL &&fast->next != NULL)

{

stack.push(slow->data);

slow = slow->next;

fast = fast->next->next;

}

/*链表有奇数个元素,跳过中间元素*/

if (fast != NULL)

{

slow = slow->next;

}

while (slow != NULL)

{

int top = stack.top();

stack.pop();

/*两者不相同,则该链表不是回文序列*/

if (top != slow->data)

{

return false;

}

slow = slow->next;

}

return true;

}

Result isPalindromeRecurse(LinkedListNode head, int length)

{

if (head == null || length == 0)

{

return new Result(null, true);

}

else if (length == 1)

{

return new Result(head.next, true);

}

else

{

return new Result(head.next.next, head.data == head.next.data);

}

Result res = isPalindromeRecurse(head.next, length - 2);

if (!res.result || res.node = null)

return res;

else

{

res.result = head.data == res.node.data;

res.node = res.node.next;

return res;

}

}

boolean isPalindrome(LinkedListNode head)

{

Result p = isPalindromeRecurse(head, listSize(head));

return p.result;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-05 18:47:11

检查链表是否为回文的相关文章

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

题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案就是数组.遍历链表,用数组把数据存下来,然后再进行一次遍历,同时用数组反向地与之比较,这样就可以判断是否回文.这个方法时间复杂度是O(n),达到了要求,然而空间复杂度显然不满足要求.所以,开数组这一类的方法显然不是最佳的. 2,既然要满足O(1)的空间复杂度,我就想到了用一个变量来存储这些数据,恰好

随手练——S(n)=O(1),判断一个链表是否为“回文”

方法一:T(n)=O(n),S(n)=O(n) 走完一遍链表,每个值入栈,之后再走一遍链表,和每次弹出的栈顶进行比较. 核心: LNode *p = l->next; while (p) { s.push(p->data); p = p->next; } p = l->next; while (p) { if (p->data != s.top()) { cout << "fuck" << endl; break; } s.pop(

单链表字符串判断回文

思路 使用快慢两个指针找到链表中点,慢指针每次前进一步,快指针每次前进两步 在慢指针前进的过程中,同时修改其 next 指针,使得链表前半部分反序. 最后比较中点两侧的链表是否相等 c版本代码见 https://github.com/hkui/algo_practice/tree/master/c/linklist/palindrome_str java版本 https://github.com/andavid/leetcode-java/blob/master/note/234/README.

请判断一个链表是否为回文链表。

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public boolean isPalindrome(ListNode head) { if(head==null || head.next == null){ return true; } Lis

回文链表

题目描述 请编写一个函数,检查链表是否为回文. 给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文. 测试样例: {1,2,3,2,1} 返回:true /* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/ class Palindrome { public: bool isPalindrome(ListNode* pHea

10.回文链表

题目描述 请编写一个函数,检查链表是否为回文. 给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文. 测试样例: {1,2,3,2,1} 返回:true {1,2,3,2,3} 返回:false 代码如下: import java.util.*; /* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public cla

链表回文串判断&amp;&amp;链式A+B

有段时间没有练习了,链表回文串判断用到了栈.链式A+B将没有的项用0补充.链表有没有头节点,及结点和链表的区别,即pNode和pHead. //#include<iostream> //using namespace std; // //class Base { //public: // Base(int j) : i(j) {} // virtual~Base() {} // void func1() { // i *= 10; // func2(); // } // int getValu

算法题——回文链表

欢迎转载,请附出处: http://blog.csdn.net/as02446418/article/details/47137745 请编写一个函数,检查链表是否为回文. 给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文. 测试样例: {1,2,3,2,1} 返回:true {1,2,3,2,3} 返回:false import java.util.*; /* public class ListNode { int val; ListNode next = n

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

题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的值,再以此比较两个链表中的值是否相等,时间复杂度O(N),空间复杂度O(N) /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { v