Skip to content

Commit aa52836

Browse files
authored
Merge pull request #2522 from aroch17/patch-1
Cleaner code for 0225-implement-stack-using-queues.py
2 parents 296bbc5 + 5e79c43 commit aa52836

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

python/0225-implement-stack-using-queues.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ def __init__(self):
44

55
def push(self, x: int) -> None:
66
self.q.append(x)
7+
8+
for _ in range(len(self.q) - 1):
9+
self.q.append(self.q.popleft())
710

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

1314
def top(self) -> int:
14-
for i in range(len(self.q) - 1):
15-
self.push(self.q.popleft())
16-
res = self.q[0]
17-
self.push(self.q.popleft())
18-
return res
15+
return self.q[0]
1916

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

0 commit comments

Comments
 (0)