题解 | #用两个栈实现队列#
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
class Solution: def __init__(self): self.stack1 = [] self.stack2 = [] def push(self, node): # write code here self.stack1.append(node) def pop(self): # return xx # 队列:先进先出;栈:先进后出 while self.stack1: self.stack2.append(self.stack1.pop()) res=self.stack2.pop() while self.stack2: self.stack1.append(self.stack2.pop()) return res