题解 | #链表的回文结构#
链表的回文结构
http://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa
1,先找到中间节点
2.逆置中间节点以后的链表
3.让第一个节点与此时中间节点依次比较
关于单链表问题,欢迎进入我的博客,我会分享学习过程中的感悟
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 ListNodesnow=A;
struct ListNodefast=A;
while(fast&&fast->next)
{
snow=snow->next;
fast=fast->next->next;
}
struct ListNode*newhead=NULL;
struct ListNode*cur=snow;
while(cur)
{
struct ListNode*next=cur->next;
cur->next=newhead;
newhead=cur;
cur=next;
}
while(A&&newhead)
{
if(A->val!=newhead->val)
{
return false;
}
else
{
A=A->next;
newhead=newhead->next;
}
}
return true;
}
};