publicbooleanisPalindrome(ListNode head) {if (head ==null) {returntrue; }// use 2 pointer to find later halfListNode fast =head.next;ListNode slow = head;while (fast !=null&&fast.next!=null) { fast =fast.next.next; slow =slow.next; }// reverse later half of listListNode laterHalf =slow.next;slow.next=null; laterHalf =reverse(laterHalf);// compare 2 part to see if we have palindrome listwhile (laterHalf !=null) {if (head.val!=laterHalf.val) {returnfalse; } laterHalf =laterHalf.next; head =head.next; }returntrue;}privateListNodereverse(ListNode node) {ListNode pre =null;while (node !=null) {ListNode tmp =node.next;node.next= pre; pre = node; node = tmp; }return pre;}