> For the complete documentation index, see [llms.txt](https://code-snippets.hbamithkumara.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-snippets.hbamithkumara.com/leetcode/problems/1-100/reverse-integer.md).

# 7. Reverse Integer

### Description

Given a 32-bit signed integer, reverse digits of an integer.

&#x20;**Note:** Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: \[−2^31,  2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

### Constraints

### Approach

### Links

* [GeeksforGeeks](https://www.geeksforgeeks.org/reverse-digits-integer-overflow-handled/)
* [Leetcode](https://leetcode.com/problems/reverse-integer/)
* [ProgramCreek](https://www.programcreek.com/2012/12/leetcode-reverse-integer/)
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}
**Input:** 123

**Output:** 321
{% endtab %}

{% tab title="Example 2" %}
**Input:** -123

**Output:** -321
{% endtab %}

{% tab title="Example 3" %}
**Input:** 120

**Output:** 21
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="Solution 1" %}

```java
/**
 * Time complexity : O(log(x)). There are roughly log10(x) digits in x.
 * Space complexity : O(1)
 */

class Solution {
    public int reverse(int x) {
        int n = 0, flag = (x < 0)? -1: 1;
        x *= flag;
        while(x > 0) {
            int pop = x%10;
            if(n > Integer.MAX_VALUE/10 || 
               (n == Integer.MAX_VALUE/10 && pop > 2)) return 0;
            n = (n*10) + pop;
            x /= 10;
        }
        return n * flag;
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*
