快慢指针+反转链表
bool isPalindrome(ListNode* head) {
if(!head||!head->next)
return true;
ListNode* fast = head;ListNode* slow = head;
while(fast&&fast->next)
{
fast = fast->next->next;
slow = slow->next;
}
ListNode* cur = slow;ListNode *nex = NULL;ListNode *list_back = NULL;
while(cur)
{
nex = cur->next;
cur->next = list_back;
list_back = cur;
cur = nex;
}
ListNode* list_front = head;
while(list_back&&list_front)
{
if(list_back->val==list_front->val)
{
list_back = list_back->next;
list_front = list_front->next;
}
else
return false;
}
return true;
}