-- coding:utf-8 --
用两个栈stack实现一个队列queue
class Solution:
def init(self):
self.stack1 = list()
self.stack2 = list()
def push(self, node):
# write code here
self.stack1.append(node)
def pop(self):
# return xx
# 判断列表为空,直接用 if not self.stack2
if not self.stack2: # 当第二个栈为空时,把第一个栈里全部弹入第二个栈
while self.stack1:
self.stack2.append(self.stack1.pop())
return self.stack2.pop() # 返回第二个栈弹出元素