Stack<数据类型> thestack = new Stack();
thestack.push(数据);//输入数据
thestack.pop() ; //取出数据
thestack.isEmpty(); //判断是否为空栈
用Stack判断链表是否为回文串:
/**
* 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> thestack = new Stack();
ListNode h = head ;
while(h != null){
thestack.push(h.val);
h = h.next;
}
while(head != null){
if(thestack.pop() != head.val){
return false;
}
head = head.next;
}
return true;
}
}