# 155. Min Stack | LeetCode | Solution

This problem is all about designing a stack data structure. Here is the problem link. See the detailed explanation there.

To solve this problem, we have to write the most common stack operations. A stack is an abstract data structure. It’s Follow the LIFO (Last In First Out) rules. I hope you already know about the stack. There are descriptions of which operations to write.

We use a list to illustrate the stack. For that, in the constructor function, we will declare an empty list. A class already created for us in LeetCode editor. We have to write the missing parts.

```def __init__(self):
"""
"""
self.stack = []```

Next, we have to write the `push()` method. When we store new data in a stack, we add it at the end of all. Data will be pass through the function argument. Let’s see the code.

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

Next, we will write the `pop()` method. This method removes the last element from the stack.

```def pop(self) -> None:
self.stack.pop()  ```

Our next method is `top()`. It returns the top value (the last element in the list) from the stack but does not delete it.

```def top(self) -> int:
return self.stack[-1]```

And the last method is `getMin()`. Here we will return the minimum value from our stack. Remember, we have to return value, not the element. To get the minimum value, we will use the `min()` function. Let’s see the code.

```def getMin(self) -> int:
return min(self.stack)```

Operations will always be called on non-empty stacks. That’s why we don’t need to check anything.

See the full code below.

```class MinStack:

def __init__(self):
"""
"""
self.stack = []

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

def pop(self) -> None:
self.stack.pop()

def top(self) -> int:
return self.stack[-1]

def getMin(self) -> int:
return min(self.stack)```

If you submit the code in LeetCode, it will be accepted.