请判断一个链表是否为回文链表。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode temp = head;
Stack<ListNode> stack = new Stack<>();
while (head != null) {
stack.push(head);
head = head.next;
}
int listNodeSize = stack.size();
while (stack.size() > listNodeSize / 2) {
ListNode lastestNode = stack.pop();
if (lastestNode.val != temp.val) {
return false;
}
temp = temp.next;
}
return true;
}
}