Min Stack LeetCode Solution – Queslers

Problem – Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Implement the MinStack class:

  • MinStack() initializes the stack object.
  • void push(int val) pushes the element val onto the stack.
  • void pop() removes the element on the top of the stack.
  • int top() gets the top element of the stack.
  • int getMin() retrieves the minimum element in the stack.

You must implement a solution with O(1) time complexity for each function.

Example 1:



MinStack minStack = new MinStack();
minStack.getMin(); // return -3
minStack.top();    // return 0
minStack.getMin(); // return -2


  • -231 <= val <= 231 - 1
  • Methods poptop and getMin operations will always be called on non-empty stacks.
  • At most 3 * 104 calls will be made to pushpoptop, and getMin.

Min Stack LeetCode Solution in Java

class MinStack {
	private Node head;
    public void push(int x) {
        if (head == null) 
            head = new Node(x, x, null);
            head = new Node(x, Math.min(x, head.min), head);
    public void pop() {
        head = head.next;
    public int top() {
        return head.val;
    public int getMin() {
        return head.min;
    private class Node {
        int val;
        int min;
        Node next;
        private Node(int val, int min, Node next) {
            this.val = val;
            this.min = min;
            this.next = next;

Min Stack LeetCode Solution in C++

class MinStack {
    stack<int> s1;
    stack<int> s2;
    void push(int x) {
	    if (s2.empty() || x <= getMin())  s2.push(x);	    
    void pop() {
	    if (s1.top() == getMin())  s2.pop();
    int top() {
	    return s1.top();
    int getMin() {
	    return s2.top();

Min Stack LeetCode Solution in Python

class MinStack:

def __init__(self):
    self.q = []

# @param x, an integer
# @return an integer
def push(self, x):
    curMin = self.getMin()
    if curMin == None or x < curMin:
        curMin = x
    self.q.append((x, curMin));

# @return nothing
def pop(self):

# @return an integer
def top(self):
    if len(self.q) == 0:
        return None
        return self.q[len(self.q) - 1][0]

# @return an integer
def getMin(self):
    if len(self.q) == 0:
        return None
        return self.q[len(self.q) - 1][1]
Min Stack LeetCode Solution Review:

In our experience, we suggest you solve this Min Stack LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Min Stack LeetCode Solution

Find on LeetCode


I hope this Min Stack LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.