class Solution {
public boolean isPalindrome(ListNode head) {
ListNode mid = middleNode(head);
ListNode tail=reverseList(mid.next);
ListNode curr = head;
while (curr!=mid){
curr=curr.next;
}
curr.next=null;
while (head!=null&&tail!=null){
if(head.val!=tail.val){
return false;
}
head=head.next;
tail=tail.next;
}
return true;
}
public ListNode middleNode(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 ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
}