/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
//快慢指针找中间节点
ListNode* FindMid(ListNode* head)
{
ListNode* fast=head;
ListNode* slow=head;
while(fast->next!=NULL&&fast!=NULL)
{
slow=slow->next;
fast=fast->next->next;
}
return slow;
}
ListNode* reList(ListNode* head)//三指针反转链表
{
if(head==NULL)
return head;
ListNode* n1=NULL,*n2=head,*n3=head->next;
while(n2!=NULL)
{
n2->next=n1;
n1=n2;
n2=n3;
if(n3!=NULL)
n3=n3->next;
}
return n1;
}
class PalindromeList {
public:
bool chkPalindrome(ListNode* A) {
ListNode* mid=FindMid(A);//找中间节点
ListNode* newhead=reList(mid);//反转后新节点
while(newhead!=NULL)
{
if(newhead->val!=A->val)
return false;
else
{
newhead=newhead->next;
A=A->next;
}
}
return true;
}
};