/**
* 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 boolean isPalindrome(ListNode head) {
Stack<Integer> stack = new Stack<>();
for(ListNode pNode = head; pNode != null; pNode = pNode.next)
stack.push(pNode.val);
for(ListNode pNode = head; pNode != null; pNode = pNode.next)
if(pNode.val != stack.pop())
return false;
return true;
}
}
|