
ListNode* ReverseList(ListNode *p) {
if (p == NULL || p->next == NULL)
return p;
ListNode *pre = NULL;
ListNode *next = p->next;
while (p) {
p->next = pre;
pre = p;
p = next;
next = p ? p->next : NULL;
}
return pre;
}
bool isPalindrome(ListNode* head) {
if (head == NULL || head->next == NULL)
return true;
ListNode *slow = head, *fast = head;
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}
slow->next = ReverseList(slow->next);
slow = slow->next;
while (slow) {
if (slow->val != head->val)
return false;
slow = slow->next;
head = head->next;
}
return true;
}