• 一个比较简单的方法:利用队列和栈的性质
  • 队列:先进先出;栈:后进先出
  • 对队列和栈进行出队和出栈,然后进行比较
  • 不相同返回false即可
    public class Palindrome {
      public boolean isPalindrome(ListNode pHead) {
          // write code here
          ListNode head = pHead;
          Queue<Integer> queue = new LinkedList<Integer>();// 队列,先进先出
          Stack stack = new Stack();// 栈,先进后出
          while(head != null){
              queue.offer(head.val);
              stack.push(head.val);
              head=head.next;
          }
          while(!stack.empty()){
              if(stack.pop()!=queue.poll()) return false;// 分别出队出栈进行比较
          }
          return true;
      }
    }