if (head == null || head.next == null) returntrue;
// 核心在于,用两个指针,slow 一边走,一边 reverse ListNode fast = head; ListNode slow = head; ListNode pre = null;
while (fast != null && fast.next != null) { // fast 的判断
fast = fast.next.next;
// 下面四行为 reverse link 的基本操作 ListNode tmp = slow.next; slow.next = pre; pre = slow; slow = tmp; }
// 如果长度为奇数 (那么slow再移一格,跳过中间的那个点) if (fast != null) slow = slow.next;
// pre is the New Head while (pre != null) { // 用 (slow != null) 一样的 if (slow.val != pre.val) returnfalse; slow = slow.next; pre = pre.next; } returntrue; }