Given a singly linked list, determine if it is a palindrome.
用常数变量解决的思路是:找到链表中点,翻转后半部分,从头开始遍历两个链表,
如果某个节点的值不相等,返回false。
需要注意的是链表只有一个或两个元素的情况。
public boolean isPalindrome(ListNode head) {
if(head == null || head.next==null)
return true;
//find list center
ListNode fast = head;
ListNode slow = head;
while(fast.next!=null && fast.next.next!=null){
fast = fast.next.next;
slow = slow.next;
}
ListNode secondHead = slow.next;
slow.next = null;
//reverse second part of the list
ListNode p1 = secondHead;
ListNode p2 = p1.next;
while(p1!=null && p2!=null){
ListNode temp = p2.next;
p2.next = p1;
p1 = p2;
p2 = temp;
}
secondHead.next = null;
//compare two sublists now
ListNode p = (p2==null?p1:p2);
ListNode q = head;
while(p!=null){
if(p.val != q.val)
return false;
p = p.next;
q = q.next;
}
return true;
}