https://leetcode-cn.com/problems/implement-stack-using-queues/
挺简单的
就是每次添加的时候,把所有的数入列临时队列
这样添加的数就到队头了。
class MyStack {
LinkedList<Integer> myStack;
LinkedList<Integer> temp;
public MyStack() {
myStack = new LinkedList<>();
temp = new LinkedList<>();
}
public void push(int x) {
while(!myStack.isEmpty()){
temp.add(myStack.poll());
}
myStack.add(x);
while(!temp.isEmpty()){
myStack.add(temp.poll());
}
}
public int pop() {
return myStack.poll();
}
public int top() {
return myStack.peek();
}
public boolean empty() {
return myStack.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/