大家好,我是苏貝,本篇博客带大家刷题,如果你觉得我写的还不错的话,可以给我一个赞👍吗,感谢❤️

思路:


1.找中间节点
找中间节点的方法在下面这个博文中详细提过
【点击进入:【leetcode】链表的中间节点 】
2.反转中间节点后面的链表
反转链表也讲过
【点击进入:【leetcode】反转链表 】
c
class PalindromeList {
public:
bool chkPalindrome(ListNode* head) {
//1.找到中间节点
ListNode* slow=head;
ListNode* fast=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
}
//2.反转中间节点后面的链表
ListNode* n1=nullptr;
ListNode* n2=slow;
ListNode* n3=n2->next;
while(n2)
{
n2->next=n1;
n1=n2;
n2=n3;
if(n3)
n3=n3->next;
}
//3.比较
while(head&&n1)
{
if(head->val!=n1->val)
return false;
head=head->next;
n1=n1->next;
}
return true;
}
};
好了,那么本篇博客就到此结束了,如果你觉得本篇博客对你有些帮助,可以给个大大的赞👍吗,感谢看到这里,我们下篇博客见❤️