/**
 * struct ListNode {
 *    int val;
 *    struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    bool isPail(ListNode* head) {
        // write code here
        if(head == nullptr || head->next == nullptr) return true;
        ListNode* slow = head;
        ListNode* fast = head;
        while(fast && fast->next){
            slow = slow->next;
            fast = fast->next->next;
        }
        //slow = slow->next;
        ListNode* cur = reverse(slow);
        fast = head;
        while(cur && fast){
            if(cur->val != fast->val) return false;
            cur = cur->next;
            fast = fast->next;
        }
        return true;
    }
    ListNode* reverse(ListNode* head){
        ListNode* pre = nullptr;
        ListNode* cur = head;
        ListNode* nex = nullptr;
        while(cur){
            nex = cur->next; 
            cur->next = pre;
            pre = cur;
            cur = nex;
        }
        return pre;
    }
};