Prove safety offer_ queue (reproduced) with two stacks

Two stacks queue

Here Insert Picture Description
Reprinted from: https: //blog.csdn.net/fuxuemingzhu/article/details/79499461

# -*- coding:utf-8 -*-
class Solution:
    def __init__(self):
        self.stack1 = []
        self.stack2 = []

    def push(self, node):
        self.stack1.append(node)

    def pop(self):
        if self.stack2:
            return self.stack2.pop()
        else:
            while self.stack1:
                self.stack2.append(self.stack1.pop())
            return self.stack2.pop()
Published 31 original articles · won praise 0 · Views 743

Guess you like

Origin blog.csdn.net/freedomUSTB/article/details/104974542