---
description: '# Easy'
---

# 7. Reverse Integer

{% hint style="info" %}
Easy. How to improve efficiency?
{% endhint %}

{% tabs %}
{% tab title="Python" %}
```python
class Solution:
    def reverse(self, x: int) -> int:
        # edge case
        Min = -(1 << 31)
        Max = (1 << 31) - 1
        if x < Min and x > Max:
            return 0
        if x == 0:
            return 0
        
        y = abs(x)
        d = y
        r = y
        digits = []
        result = 0
        flag = 0
        while d != 0:
            r = d%10
            d = d//10
            if(r != 0 or flag != 0):
                flag = 1
                result = result*10+r
            
        # result = 0
        # for i in digits:
        #     result = result*10 + i
            
        if x < 0:
            result = -result
        
        # return
        if result < Min or result > Max:
            return 0
        else:
            return result
```
{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}
