class Solution {
public:
ListNode* reverse(ListNode* head)//翻转以head为头节点的链表
{
if(!head||!head->next) return head;
auto a=head,b=head->next;
while(b)
{
auto c=b->next;
b->next=a;
a=b;
b=c;
}
head->next=nullptr;
return a;
}
bool isPalindrome(ListNode* head) {
if(!head||!head->next) return head;
auto slow=head,quick=head;
while(quick->next&&quick->next->next)//快慢指针查找中点
{
slow=slow->next;
quick=quick->next->next;
}
//此时slow指向中点
auto r=reverse(slow->next),l=head;
slow->next=NULL;
while(l&&r)
{
if(l->val!=r->val) return false;
l=l->next;
r=r->next;
}
return true;
}
};
标签:head,slow,return,auto,next,链表,quick,234,LeetCode
From: https://www.cnblogs.com/tangxibomb/p/17547188.html