链表中间节点开始反转查找
链表的回文结构
https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
ListNode*mid(ListNode*head)
{
ListNode*fast,*lose;
fast=lose=head;
while(fast&&fast->next)
{
fast=fast->next->next;
lose=lose->next;
}
return lose;
}
ListNode* reverse(ListNode*phead)
{
ListNode*newhead=NULL,*pcur=phead,*next=pcur->next;
while(next)
{
pcur->next=newhead;
newhead=pcur;
pcur=next;
next=next->next;
}
pcur->next=newhead;
return pcur;
}
class PalindromeList {
public:
bool chkPalindrome(ListNode* A) {
if(!A)
return false;
// write code here
ListNode*pmid=mid(A);
ListNode*newhead=reverse(pmid);
ListNode*p1=A,*p2=newhead;
while(p1&&p2)
{
if(p1->val!=p2->val)
return false;
p1=p1->next;
p2=p2->next;
}
return true;
}
};
