Reverse sec half of list, compare first half;
public boolean isPalindrome(ListNode head) {
public boolean isPalindrome(ListNode head) {
if(head==null||head.next==null) return true;
ListNode fast = head.next, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
slow = reverse(slow);
while (slow != null&&head!=null) {
if (slow.val != head.val) {
return false;
}
head = head.next;
slow = slow.next;
}
return true;
}
public ListNode reverse(ListNode head) {
if(head==null){
return null;
}
ListNode p1 = head;
ListNode p2 = p1.next;
head.next = null;
while(p1!=null&&p2!=null){
ListNode tmp = p2.next;
p2.next = p1;
p1 = p2;
p2 = tmp;
}return p1;
}