方法1
分析
代码
class PalindromeList {
public:
ListNode* findmid(ListNode*A)
{
ListNode*fast=A;
ListNode*slow=A;
while(fast && fast->next)
{
fast=fast->next->next;
slow=slow->next;
}
return slow;
}
ListNode* reverse(ListNode*mid)
{
ListNode*cur=mid,*newhead=NULL;
while(cur)
{
ListNode*next =cur->next;
cur->next=newhead;
newhead=cur;
cur=next;
}
return newhead;
}
bool chkPalindrome(ListNode* A) {
ListNode* mid=findmid(A);
ListNode* head1=reverse(mid);
ListNode* head2=A;
while(head1 && head2)
{
if(head1->val!=head2->val)
return false;
head1=head1->next;
head2=head2->next;
}
return true;
}
};
方法2