/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public static ListNode reverseLL(ListNode head) { ListNode cur = head; ListNode prev = null; ListNode next = null; while (cur != null) { next = cur.next; cur.next = prev; prev = cur; cur = next; } return prev; } public static ListNode midPointLL (ListNode head) { ListNode slow = head; ListNode fast = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } public boolean isPalindrome(ListNode head) { ListNode midNode = midPointLL(head); ListNode temp = midNode.next; midNode.next = null; ListNode newHead = reverseLL(temp); ListNode t1 = head; ListNode t2 = newHead; while (t1 != null && t2 != null) { if (t1.val != t2.val) return false; t1 = t1.next; t2 = t2.next; } return true; } }