题解 | #两个队列实现栈#
两个队列实现栈
https://www.nowcoder.com/practice/9fc5ae0e203f4d079b68dee34818832a
import java.util.*; public class Solution { Queue<Integer> queue1 = new LinkedList(); Queue<Integer> queue2 = new LinkedList(); public void push(int x){ while(!queue1.isEmpty()){ queue2.add(queue1.poll()); } queue1.add(x); while(!queue2.isEmpty()){ queue1.add(queue2.poll()); } } public int pop(){ return queue1.poll(); } public int top(){ return queue1.peek(); } public boolean empty(){ return queue1.isEmpty(); } }