import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
public boolean isPail(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode curr1 = reverse(slow);
ListNode curr2 = head;
while (curr1 != null) {
if (curr1.val != curr2.val) {
return false;
}
curr1 = curr1.next;
curr2 = curr2.next;
}
return true;
}
private ListNode reverse(ListNode node) {
ListNode prev = null;
ListNode curr = node;
while (curr != null) {
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}
}