Reverse Integer LeetCode Solution

Last updated on January 5th, 2025 at 01:20 am

Here, we see a Reverse Integer LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.

Topics

Math

Companies

Apple, Bloomberg

Level of Question

Medium

Reverse Integer LeetCode Solution

Reverse Integer LeetCode Solution

1. Problem Statement

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Example 1:
Input: x = 123
Output: 321

Example 2:
Input: x = -123
Output: -321

Example 3:
Input: x = 120
Output: 21

2. Coding Pattern Used in Solution

The coding pattern used in all the provided implementations is “Number Manipulation.” This pattern involves performing mathematical operations on numbers to achieve the desired result. It focuses on reversing the digits of an integer and handling edge cases like overflow.

3. Code Implementation in Different Languages

3.1 Reverse Integer C++

class Solution {
public:
    int reverse(int x) {
        long r=0;
        while(x){
         r=r*10+x%10;
         x=x/10;
        }
        if(r>INT_MAX || r<INT_MIN) return 0;
        return int(r);      
    }
};

3.2 Reverse Integer Java

class Solution {
    public int reverse(int x) {
        long finalNum = 0;
        while(x!=0){
            int lastDig = x%10;
            finalNum += lastDig;
            finalNum = finalNum*10;
            x= x/10;
        }
        finalNum = finalNum/10;
        if(finalNum > Integer.MAX_VALUE || finalNum<Integer.MIN_VALUE){
            return 0;
        }
        if(x<0){
            return (int)(-1*finalNum);
        }
        return (int)finalNum;        
    }
}

3.3 Reverse Integer JavaScript

var reverse = function(x) {
  const absReversed = Math.abs(x).toString().split('').reverse().join('');
  if (absReversed > 2**31) return 0;
  return absReversed * Math.sign(x);    
};

3.4 Reverse Integer Python

class Solution(object):
    def reverse(self, x):
        reverse = 0
        sign = -1 if x < 0 else 1
        x = abs(x)
        while x:
            digit = x % 10
            reverse = reverse * 10 + digit
            x /= 10
        result = sign * reverse
        if result > 2 ** 31 - 1 or result < -(2 ** 31):
            return 0
        return result

4. Time and Space Complexity

Time ComplexitySpace Complexity
C++O(log₁₀(x))O(n)
JavaO(log₁₀(x))O(n)
JavaScriptO(1)O(1)
PythonO(log₁₀(x))O(n)
  • The C++, Java, and Python implementations are more efficient in terms of space compared to the JavaScript implementation.
  • The JavaScript implementation uses string manipulation, which increases both time and space complexity.
  • All implementations handle overflow conditions and preserve the sign of the original number.

Scroll to Top