/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
ListNode *fast = head;
ListNode *slow = head;
ListNode *pre = nullptr;
ListNode *tmp = nullptr;
while(fast && fast->next){
fast = fast->next->next;
tmp = slow->next;
slow->next = pre;
pre = slow;
slow = tmp;
}
if(fast != nullptr){
slow = slow->next;
}
while(slow && pre){
if(slow->val != pre->val){
return false;
}
slow = slow->next;
pre = pre->next;
}
return true;
}
};
http://t.csdnimg.cn/hVybH