Algobase
Problems
Get Premium
Pricing
Problems
/
278. Implement Stack using Queues
Prev
Next
Visualizer
Problem
Solution
Code
Basic Operations
Multiple Pushes
Interleaved Push and Pop
⌥
Visualizer will appear here
Problem
Solution
Code
1
class MyStack:
2
def __init__(self):
3
self.q = []
4
def push(self, val):
5
self.q.append(val)
6
for _ in range(len(self.q) - 1):
7
self.q.append(self.q.pop(0))
8
def pop(self): return self.q.pop(0)
9
def top(self): return self.q[0]
Visualizer
Basic Operations
Multiple Pushes
Interleaved Push and Pop
⌥
Visualizer will appear here