↓Skip to main content
  1. LeetCode/

LeetCode 225: Implement Stack using Queues

·2 mins· ·
LeetCode Easy Stack Design Queue
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-21

  • Submissions: Runtime Error → Wrong Answer ×2 → Accepted

Approach
#

Only queue operations are allowed (append at the back, remove from the front). After push(x), move the n - 1 elements ahead of x from the front to the back, leaving x at the front (Queue and Design). Then pop and top read the front in O(1), and push costs O(n).

My Attempts
#

I used a Python list as the stack outright, without implementing it on queues as asked. The two WAs in between returned self.l[0] (the first element pushed) from top instead of self.l[-1].

Solution
#

class MyStack:
    def __init__(self):
        self.q = deque()

    def push(self, x: int) -> None:
        self.q.append(x)
        for _ in range(len(self.q) - 1):
            self.q.append(self.q.popleft())

    def pop(self) -> int:
        return self.q.popleft()

    def top(self) -> int:
        return self.q[0]

    def empty(self) -> bool:
        return not self.q

Complexity
#

push is O(n), the rest O(1); space O(n).

Mistakes to Avoid
#

  • The problem asks for queue operations only; using a list as a stack passes but misses the point.
  • The stack top is the last element pushed, not the first.

Interview Explanation
#

With queue operations only, after each push I rotate the earlier elements to the back so the new element is at the front, making pop and top read the front. Push is O(n), the rest O(1).

Related

LeetCode 145: Binary Tree Postorder Traversal
·2 mins
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 145 note: iterative post-order pushes a node back with a visited flag on its first pop, then the right and left children; record it on the second pop
LeetCode 144: Binary Tree Preorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 144 note: iterative pre-order traversal records on pop and pushes the right child before the left so the left is handled first
LeetCode 94: Binary Tree Inorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 94 note: iterative in-order traversal, pushing nodes while going left, recording on pop, then turning to the right subtree
LeetCode 171: Excel Sheet Column Number
·1 min
LeetCode Easy Math String
LeetCode 171 note: scan left to right with res = res × 26 + letter value (A = 1)
LeetCode 202: Happy Number
·1 min
LeetCode Easy Hash-Table Math Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 202 note: repeatedly take the sum of squared digits, which reaches 1 or enters a cycle; detect it with a set of seen numbers or fast and slow pointers
LeetCode 203: Remove Linked List Elements
·2 mins
LeetCode Easy Linked-List Recursion
LeetCode 203 note: the head may be deleted too, so a dummy head turns every deletion into deleting cur.next; do not advance after deleting