入栈出栈

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    /*public boolean isPail (ListNode head) {
        // write code here
        if(head == null || head.next == null){
            return true;
        }
        int len = length(head);
        //System.out.println(len);
         ListNode flag = head;
        for(int i = 0;i<len/2;i++){
            ListNode last = flag;
            for(int j = 0;j<len-i-1;j++){
                last = last.next;
            }
            if(head.val != last.val){
                //System.out.println(last.val);
                return false;
            }
            head = head.next;
        }
        return true;
    }
    public int length(ListNode head){
        int flag = 0;
        while(head != null){
            flag++;
            head = head.next;
        }
        return flag;
    }*/
    public boolean isPail(ListNode head) {
    ListNode temp = head;
    Stack<Integer> stack = new Stack();
    //把链表节点的值存放到栈中
    while (temp != null) {
        stack.push(temp.val);
        temp = temp.next;
    }

    //然后再出栈
    while (head != null) {
        if (head.val != stack.pop()) {
            return false;
        }
        head = head.next;
    }
    return true;
}
}