Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1425 from fulcus/patch-1
Browse files Browse the repository at this point in the history
Create 232-Implement-Queue-Using-Stacks.py
  • Loading branch information
Ahmad-A0 authored Dec 22, 2022
2 parents d7bda02 + 8027f93 commit c494792
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions python/232-Implement-Queue-Using-Stacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class MyQueue:

def __init__(self):
self.append_stack = []
self.inverted_stack = []

def push(self, x: int) -> None:
self.append_stack.append(x)

def pop(self) -> int:
if not self.inverted_stack:
while self.append_stack:
self.inverted_stack.append(self.append_stack.pop())
return self.inverted_stack.pop()

def peek(self) -> int:
if not self.inverted_stack:
while self.append_stack:
self.inverted_stack.append(self.append_stack.pop())
return self.inverted_stack[-1]

def empty(self) -> bool:
return not (self.append_stack or self.inverted_stack)

0 comments on commit c494792

Please sign in to comment.