if (head == null ){
return true;
}
if (head.next == null){
return true;
}
Stack<ListNode> stack = new Stack<>();
ListNode slow = head;
ListNode fast = head;
while (fast != null) {
stack.push(slow);
if (fast.next == null){
break;
}
fast = fast.next.next;
slow=slow.next;
}
while (slow != null){
int var = slow.val;
ListNode tmp = stack.pop();
if (var != tmp.val){
return false;
}
slow = slow.next;
}
return true;
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null)
return true;
ListNode slow = head;
ListNode fast = head;
ListNode pre = null;
while (fast != null && fast.next != null) {
ListNode cur = slow;
slow = slow.next;
fast = fast.next.next;
cur.next = pre;
pre = cur;
}
if (fast != null) slow = slow.next;
while (pre != null) {
if (pre.val != slow.val)
return false;
slow = slow.next;
pre = pre.next;
}
return true;
}