ここでパリティを判断する必要はない。1->2->1 ==> 1->2<-1
1->3->3->1==>1->3 3<-1
public class Num0206回文 {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) return true;
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode pre = null;
while (slow != null) {
ListNode next = slow.next;
slow.next = pre;
pre = slow;
slow = next;
}
ListNode cur = head;
while (pre != null) {
if (pre.val != cur.val) return false;
pre = pre.next;
cur = cur.next;
}
return true;
}
}