Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1724 from AP-Repositories/patch-10
Browse files Browse the repository at this point in the history
Create 0225-implement-stack-using-queues.py
  • Loading branch information
Ahmad-A0 authored Jan 1, 2023
2 parents 52693d4 + 14bd57d commit 1a340d8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions python/0225-implement-stack-using-queues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class MyStack:
def __init__(self):
self.q = deque()

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

def pop(self) -> int:
for i in range(len(self.q) - 1):
self.push(self.q.popleft())
return self.q.popleft()

def top(self) -> int:
for i in range(len(self.q) - 1):
self.push(self.q.popleft())
res = self.q[0]
self.push(self.q.popleft())
return res

def empty(self) -> bool:
return len(self.q) == 0

0 comments on commit 1a340d8

Please sign in to comment.