publicbooleanisPalindrome(ListNode head) {
if(head ==null|| head.next==null)
returntrue;
//find list middle = slow pointer ListNode fast = head;
ListNode slow = head;
while(fast!=null&& fast.next!=null){
fast = fast.next.next;
slow = slow.next;
}
//reverse second part of the list ListNode prev =null;
while(slow!=null){
ListNode nxt = slow.next;
slow.next= prev;
prev = slow;
slow = nxt;
}
// Check palindrome ListNode left = head, right = prev;
// we are using right as for odd numbered list// right side will be smallerwhile(right!=null){
if(left.val!= right.val)
returnfalse;
left = left.next;
right = right.next;
}
returntrue;
}
publicclassSolution {
ListNode left;
publicbooleanisPalindrome(ListNode head) {
left = head;
boolean result = helper(head);
return result;
}
publicbooleanhelper(ListNode right){
//stop recursionif (right ==null)
returntrue;
//if sub-list is not palindrome, return falseboolean x = helper(right.next);
if (!x)
returnfalse;
//current left and rightboolean y = (left.val== right.val);
//move left to next left = left.next;
return y;
}
}