# 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**

*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://code-snippets.hbamithkumara.com/leetcode/problems/1-100/reverse-integer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
