空间复杂度O(n)
时间复杂度O(n)
fast走两步slow走一步,fast走完的时候slow到中间点
把中间点之后的链表翻转
fast回到头,和slow一起走(每次走一步)
如果回文,从头走的和从中间走的每个val值都相同

import java.util.*;

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class PalindromeList {
    public boolean chkPalindrome(ListNode A) {
        // write code here
        ListNode fast = A;
        ListNode slow = A;
        ListNode prev = null;
        //找到中间位置
        while(null != fast && null != fast.next) {
            prev = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        fast = slow;
        //反转后半截链表
        slow = reverseList(fast);
        //把反转后的链表和前面的连接起来
        prev.next = slow;
        //fast回到初始位置
        fast = A;
        while(null!=slow) {
            if(slow.val!=fast.val) {
                return false;
            }
            slow = slow.next;
            fast = fast.next;
        }
        return true;
    }
    public ListNode reverseList(ListNode head) {
        if(null == head || null == head.next) {
            return head;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while(null!=cur) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        head.next = null;
        return prev;
    }
}