import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    public boolean isPail (ListNode head) {
        // write code here
        ListNode slow = head;
        for (ListNode fast = head; fast.next != null && fast.next.next != null; fast = fast.next.next) {
            slow = slow.next;   // 寻找中点
        }
        // 翻转后一段
        ListNode pre = null, temp;
        for (ListNode cur = slow.next; cur != null; cur = temp) {
            temp = cur.next;
            cur.next = pre;
            pre = cur;
        }
        // 判断从 head 开始和 pre 开始 是否是回文
        while (head != null && pre != null) {
            if (head.val != pre.val) {
                return false;
            }
            head = head.next;
            pre = pre.next;
        }
        return true;  // 多一个也是回文
    }
}