/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */

/**
 * 
 * @param head ListNode类 the head
 * @return bool布尔型
 */
function isPail(head) {
    // write code here
    // 复制列表
    function geNode(list) {
        let pre = new ListNode(0);
        let node = pre;
        while (list) {
            pre.next = new ListNode(list.val);
            pre = pre.next;
            list = list.next;
        }
        return node.next;
    }
    // 反转列表
    function getInv(list) {
        let end = list;
        cur = list;
        pre = null;
        while (end) {
            end = end.next;
        }
        while (cur != end) {
            let temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
    let arr = geNode(head);
    let res = getInv(head);
    // 比较列表反转前后是否一样
    while (arr) {
        if (arr.val != res.val) {
            return false;
        }
        res = res.next;
        arr = arr.next;
    }
    return true;
}
module.exports = {
    isPail: isPail
};