class Solution {
public boolean isPalindrome(ListNode head){
ListNode slow = head, fast = head;
Stack<Integer> stack = new Stack<>();
while(fast != null && fast.next != null){
stack.push(slow.val);
slow = slow.next;
fast = fast.next.next;
}
if(fast != null) slow = slow.next;
while(slow != null){
if(slow.val != stack.pop()) return false;
slow = slow.next;
}
return true;
}
}
Explanation: Push first half into a stack, then compare with second half values for palindrome check.
β± O(n) | πΎ O(n)