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
Constraints:
-231 <= x <= 231 - 1Problem Overview: Given a 32-bit signed integer x, reverse its digits. If reversing the number causes it to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], return 0. The main challenge is extracting digits efficiently and detecting overflow before it happens.
Approach 1: Iterative Division and Modulus (O(log n) time, O(1) space)
This method processes the integer digit by digit using arithmetic operations. Repeatedly extract the last digit using x % 10, then remove it from the number using integer division x //= 10. Build the reversed value by multiplying the current result by 10 and adding the extracted digit. The critical detail is overflow detection: before updating rev = rev * 10 + digit, check whether rev would exceed the 32-bit limit. If it does, return 0. The loop runs once per digit, giving O(log n) time since the number of digits grows logarithmically with the integer size, and O(1) space because only a few variables are used.
This approach relies purely on arithmetic and fits naturally with problems involving math operations and integer manipulation. Interviewers usually expect this version because it demonstrates control over numeric operations, edge cases, and overflow handling.
Approach 2: String Conversion and Manipulation (O(d) time, O(d) space)
Another option converts the integer to a string, reverses the characters, then converts the result back to an integer. First capture the sign, convert the absolute value of x into a string, and reverse it using slicing or built‑in utilities. After reversing, convert it back to an integer and reapply the original sign. Finally, check if the result lies within the 32-bit signed range. This method is straightforward because reversing a string is trivial, but it introduces extra memory usage proportional to the number of digits.
This technique is often used in scripting languages where string operations are convenient. It touches both string manipulation and numeric processing. The complexity remains linear in the number of digits (O(d) time) with O(d) auxiliary space for the reversed string.
Recommended for interviews: The iterative division and modulus approach is the expected solution. It shows that you understand how to extract digits, rebuild numbers, and guard against overflow in constant space. The string-based solution works and is easy to write, but interviewers often treat it as a convenience approach rather than demonstrating strong control over numeric algorithms.
This method involves extracting digits one at a time from the integer using modulus and division operations. By repeatedly taking the last digit of the number, you can construct the reversed integer. However, special care must be taken to handle the overflow condition, as reversing an integer can potentially produce a value beyond the 32-bit signed integer limit.
This C solution uses a while loop to pop digits from the input number x and push them onto the result. Overflow conditions are checked both before multiplication and addition.
C++
Java
Python
C#
JavaScript
Time Complexity: O(log10(x)) because we are processing each digit once.
Space Complexity: O(1) since we are using constant extra space.
In this approach, the integer is first converted to a string representation, reversed as a string, and then converted back to an integer. This method mitigates overflow issues by checking the integer post-conversion if it falls within the valid range.
This Python solution converts the integer x to a string, reverses it, and then converts it back to an integer. The sign is preserved and checked for overflow before returning the result.
JavaScript
Time Complexity: O(n) where n is the number of digits in x.
Space Complexity: O(n) due to the use of the string to store digits temporarily.
| Approach | Complexity |
|---|---|
| Iterative Division and Modulus | Time Complexity: O(log10(x)) because we are processing each digit once. |
| String Conversion and Manipulation | Time Complexity: O(n) where n is the number of digits in x. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Division and Modulus | O(log n) | O(1) | Preferred interview solution. Works without extra memory and handles overflow checks explicitly. |
| String Conversion and Manipulation | O(d) | O(d) | Quick implementation in languages with strong string utilities. |
Lecture 7: LeetCode Problem Solving Session • CodeHelp - by Babbar • 921,595 views views
Watch 9 more video solutions →Practice Reverse Integer with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor