1,先找到中间节点,
2.从中间节点以后开始逆置
3.然后从中间节点向后迭代
4.如果是回文,返回true,否则返回false
5.欢迎大家访问我的博客,我也会分享我自己的学习之路
https://blog.csdn.net/m0_63111921/article/details/122463378?spm=1001.2014.3001.5501
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:
bool chkPalindrome(ListNode* A)
{
struct ListNode*slow=A;
struct ListNode*fast=A;
while(fast->next)
{
slow=slow->next;
fast=fast->next;
}
struct ListNode*change=NULL;
struct ListNode*head=slow;
while(head)
{
struct ListNode*next=slow->next;
head->next=change;
change=head;//重新置头
head=next;
}
while(slow)
{
if(A->val!=change->val)
{
return false;
}
slow=slow->next;
change=change->next;
}
return true;
}
};

京公网安备 11010502036488号