(https://leetcode-cn.com/problems/aMhZSa/))

    1. public class 剑指Offer_II_027_回文链表 {
    2. public boolean isPalindrome(ListNode head) {
    3. if (head==null) return false;
    4. Stack<ListNode> stack = new Stack<ListNode>();
    5. ListNode node = head;
    6. while (node!=null) {
    7. stack.push(node);
    8. node = node.next;
    9. }
    10. int size = stack.size();
    11. node = stack.pop();
    12. for (int i = 0; i < size >> 1; i++) {
    13. if (!(node.val == head.val)) return false;
    14. node = stack.pop();
    15. head = head.next;
    16. }
    17. return true;
    18. }
    19. }

    利用了栈的先进后出特性,如果是回文链表,入栈时正序 的元素和出栈时倒序的元素应该是一样的。