classCQueue{
Stack stack;publicCQueue(){this.stack =newStack();}publicvoidappendTail(int value){this.stack.push(value);}publicintdeleteHead(){if(this.stack.empty())return-1;
Stack tmp =newStack();while(!this.stack.empty()){
tmp.push(this.stack.pop());}int res =(int) tmp.pop();while(!tmp.empty()){this.stack.push(tmp.pop());}return res;}}/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
4.题解代码
classCQueue{
LinkedList<Integer> A, B;publicCQueue(){
A =newLinkedList<Integer>();
B =newLinkedList<Integer>();}publicvoidappendTail(int value){
A.addLast(value);}publicintdeleteHead(){if(!B.isEmpty())return B.removeLast();if(A.isEmpty())return-1;while(!A.isEmpty())
B.addLast(A.removeLast());return B.removeLast();}}