Reversing an integer means to reverse all its digits.
2021 gives 1202. Reversing 12300 gives 321 as the leading zeros are not retained.Given an integer num, reverse num to get reversed1, then reverse reversed1 to get reversed2. Return true if reversed2 equals num. Otherwise return false.
Example 1:
Input: num = 526 Output: true Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.
Example 2:
Input: num = 1800 Output: false Explanation: Reverse num to get 81, then reverse 81 to get 18, which does not equal num.
Example 3:
Input: num = 0 Output: true Explanation: Reverse num to get 0, then reverse 0 to get 0, which equals num.
Constraints:
0 <= num <= 106Problem Overview: You’re given a non‑negative integer num. Reverse its digits once, then reverse the result again. The task is to check whether the final value equals the original number. The catch comes from how integer reversal handles leading zeros.
When a number is reversed, any leading zeros disappear. For example, reversing 120 produces 21. Reversing 21 again gives 12, which is different from the original 120. This behavior drives the entire solution.
Approach 1: Direct Reversal Approach (O(d) time, O(1) space)
This approach simulates exactly what the problem describes. Write a helper function that reverses an integer by repeatedly extracting digits using % 10 and building the reversed value with multiplication by 10. First reverse num, then reverse the result again. Finally compare the double‑reversed value with the original number.
The algorithm performs digit operations in a loop until the number becomes zero. If the number has d digits, each reversal costs O(d), making the total still O(d). Space remains O(1) since only a few integer variables are used. This approach is straightforward and mirrors the problem statement, making it a good baseline when reasoning about math operations and digit manipulation.
Approach 2: Optimized Special Case Handling (O(1) time, O(1) space)
The key insight: only numbers with trailing zeros change after a double reversal. If a number ends with 0, the first reversal removes that zero because integers cannot store leading zeros. Once the zero disappears, the second reversal cannot restore it.
Example: num = 120. First reversal → 21. Second reversal → 12. The original value cannot be recovered. In contrast, a number like 123 becomes 321 and then 123, which matches the original.
This leads to a constant‑time rule: return true if num == 0 or num % 10 != 0. Zero is a special case because reversing it still gives zero. This approach relies purely on digit properties rather than explicit reversal, a common trick in math and simulation problems.
Recommended for interviews: Start with the direct reversal simulation to demonstrate understanding of digit operations. Then point out the trailing‑zero observation and switch to the constant‑time rule. Interviewers usually expect the optimized O(1) solution because it shows you recognized the mathematical property behind the operation rather than blindly simulating it.
This approach involves reversing the number, then reversing the result again. We first reverse the input number and store the reverse result. Then, we reverse the reversed result to see if we get back the original number.
The C solution uses a helper function reverse to reverse the digits of the number. The main function isSameAfterReversals takes an integer, reverses it once to get reversed1, and then reverses it again to get reversed2. It compares the result with the original number and returns true if they match, and false otherwise.
Time Complexity: O(d), where d is the number of digits in the number. We traverse each digit to reverse it.
Space Complexity: O(1), as we use a constant amount of space.
In this approach, we directly check for conditions when reversing a number twice will not change it. If a number has trailing zeros, reversing it will change its length because the trailing zeros are lost. For any number except zero, if it has no trailing zeros, reversing twice will always result in the original number.
In this C implementation, we directly return true for zero, because reversing 0 gives 0 again. For other numbers, we simply check if they end with zero, as losing the trailing zero changes the number during reversal.
Time Complexity: O(1), as we perform a constant-time check.
Space Complexity: O(1), since no additional space is needed.
If the number is 0, or the last digit of the number is not 0, then the number after reversing twice will be the same as the original number.
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
| Approach | Complexity |
|---|---|
| Direct Reversal Approach | Time Complexity: O(d), where d is the number of digits in the number. We traverse each digit to reverse it. |
| Optimized Special Case Handling | Time Complexity: O(1), as we perform a constant-time check. |
| Mathematics | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Reversal Approach | O(d) | O(1) | When implementing the exact process described or practicing digit manipulation logic. |
| Optimized Special Case Handling | O(1) | O(1) | Best solution for interviews and production code; uses the trailing-zero insight instead of full reversal. |
A Number After a Double Reversal | Leetcode 2119 | Live coding session 🔥🔥🔥🔥| 1 Liner | Contest 273 • Coding Decoded • 690 views views
Watch 9 more video solutions →Practice A Number After a Double Reversal with our built-in code editor and test cases.
Practice on FleetCode