Talentd
FleetCode
Make and follow sheets.Sign in

A Number After a Double Reversal solution and explanation

EasyMath5 min read82.7% acceptanceUpdated 9 Apr 2026
A Number After a Double Reversal | Leetcode 2119 | Live coding session 🔥🔥🔥🔥| 1 Liner | Contest 273Coding Decoded · 4:01 · 703 viewsWatch on YouTube
9 more videos on this problem
  1. A Number After a Double Reversal | Leetcode 2119 | Live coding session 🔥🔥🔥🔥| 1 Liner | Contest 273Coding Decoded · 4:01 · 703 views
  2. 2119. A Number After a Double Reversal || In Java || Leetcode Problem #leetcodeQuick Learning With Sree · 4:43 · 552 views
  3. 2119. A Number After a Double Reversal (Leetcode Easy)Programming Live with Larry · 2:28 · 425 views
  4. Leetcode 2119 A Number After a Double ReversalMath Geeks · 1:09 · 149 views
  5. A Number After a Double Reversal | Leetcode 2119 | Approach + Code with explanation.Pirai's Academy · 7:36 · 78 views
  6. Leetcode - 2119 - A Number After A Double Reversal - TypeScript / JavaScriptcoding BBQ · 3:39 · 40 views
  7. 2119. A Number After a Double Reversal - Javascript - Time: O(n) Space: O(n) - LeetcodeAaron Sadino Coding · 9:39 · 19 views
  8. LeetCode 2119 | A Number After a Double Reversal 🔥 | Easy C++ Solution + ExplanationManan patel · 8:05 · 14 views
  9. LeetCode 2119 - A Number After a Double Reversal 🔥Jal Patel · 2:54 · 13 views
  10. 🏆 A Number After a Double Reversal | LeetCode 2119 Explained 🔥code with vedant · 4:06 · 10 views

The problem

Reversing an integer means to reverse all its digits.

  • For example, reversing 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 <= 106

Approach overview

Problem 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.

Approach 1: Direct Reversal Approach

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.

def reverse(num):
    reversed_num = 0
    while num > 0:
        reversed_num = reversed_num * 10 + num % 10
        num //= 10
    return reversed_num


def is_same_after_reversals(num):
    reversed1 = reverse(num)
    reversed2 = reverse(reversed1)
    return num == reversed2


num = 526
print(is_same_after_reversals(num))
public class DoubleReversal {
  
    public static int reverse(int num) {
        int reversed = 0;
        while (num > 0) {
            reversed = reversed * 10 + num % 10;
            num /= 10;
        }
        return reversed;
    }

    public static boolean isSameAfterReversals(int num) {
        int reversed1 = reverse(num);
        int reversed2 = reverse(reversed1);
        return num == reversed2;
    }

    public static void main(String[] args) {
        int num = 526;
        System.out.println(isSameAfterReversals(num));
    }
}
#include <iostream>
using namespace std;

int reverse(int num) {
    int reversed = 0;
    while (num > 0) {
        reversed = reversed * 10 + num % 10;
        num /= 10;
    }
    return reversed;
}

bool isSameAfterReversals(int num) {
    int reversed1 = reverse(num);
    int reversed2 = reverse(reversed1);
    return num == reversed2;
}

int main() {
    int num = 526;
    cout << isSameAfterReversals(num) << endl;
    return 0;
}
function reverse(num) {
    let reversed = 0;
    while (num > 0) {
        reversed = reversed * 10 + num % 10;
        num = Math.floor(num / 10);
    }
    return reversed;
}

function isSameAfterReversals(num) {
    const reversed1 = reverse(num);
    const reversed2 = reverse(reversed1);
    return num === reversed2;
}

let num = 526;
console.log(isSameAfterReversals(num));
#include <stdbool.h>
#include <stdio.h>

int reverse(int num) {
    int reversed = 0;
    while (num > 0) {
        reversed = reversed * 10 + num % 10;
        num /= 10;
    }
    return reversed;
}

bool isSameAfterReversals(int num) {
    int reversed1 = reverse(num);
    int reversed2 = reverse(reversed1);
    return num == reversed2;
}

int main() {
    int num = 526;
    printf("%d\n", isSameAfterReversals(num));
    return 0;
}
using System;

public class Program {

    public static int Reverse(int num) {
        int reversed = 0;
        while (num > 0) {
            reversed = reversed * 10 + num % 10;
            num /= 10;
        }
        return reversed;
    }

    public static bool IsSameAfterReversals(int num) {
        int reversed1 = Reverse(num);
        int reversed2 = Reverse(reversed1);
        return num == reversed2;
    }

    public static void Main() {
        int num = 526;
        Console.WriteLine(IsSameAfterReversals(num));
    }
}

The Python solution defines a reverse function that reverses the number iteratively. The is_same_after_reversals function checks if the double-reversed number equals the original.

Complexity

Time Complexity: O(d), where d is the number of digits in num.
Space Complexity: O(1), as no additional space is used.

Try this approach in the editor

Approach 2: Optimized Special Case Handling

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.

def is_same_after_reversals(num):
    if num == 0:
        return True
    return num % 10 != 0

num = 1800
print(is_same_after_reversals(num))
public class DoubleReversalOptimized {

    public static boolean isSameAfterReversals(int num) {
        if (num == 0) return true;
        return num % 10 != 0;
    }

    public static void main(String[] args) {
        int num = 1800;
        System.out.println(isSameAfterReversals(num));
    }
}
#include <iostream>
using namespace std;

bool isSameAfterReversals(int num) {
    if (num == 0) return true;
    return num % 10 != 0;
}

int main() {
    int num = 1800;
    cout << isSameAfterReversals(num) << endl;
    return 0;
}
function isSameAfterReversals(num) {
    if (num === 0) return true;
    return num % 10 !== 0;
}

let num = 1800;
console.log(isSameAfterReversals(num));
#include <stdbool.h>
#include <stdio.h>

bool isSameAfterReversals(int num) {
    if (num == 0) return true;
    return num % 10 != 0;
}

int main() {
    int num = 1800;
    printf("%d\n", isSameAfterReversals(num));
    return 0;
}
using System;

public class Program {

    public static bool IsSameAfterReversals(int num) {
        if (num == 0) return true;
        return num % 10 != 0;
    }

    public static void Main() {
        int num = 1800;
        Console.WriteLine(IsSameAfterReversals(num));
    }
}

The Python function checks if the number is zero or ends with a zero. If it's zero, reversing hasn't changed it. Ending in a zero means its length reduces after reversal.

Complexity

Time Complexity: O(1), because we only perform constant operations.
Space Complexity: O(1), as there's no extra space requirement.

Try this approach in the editor

Approach 3: Mathematics

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).

function isSameAfterReversals(num: number): boolean {
    return num === 0 || num % 10 !== 0;
}
func isSameAfterReversals(num int) bool {
	return num == 0 || num%10 != 0
}
impl Solution {
    pub fn is_same_after_reversals(num: i32) -> bool {
        num == 0 || num % 10 != 0
    }
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Direct Reversal Approach

Time Complexity: O(d), where d is the number of digits in num.
Space Complexity: O(1), as no additional space is used.

Optimized Special Case Handling

Time Complexity: O(1), because we only perform constant operations.
Space Complexity: O(1), as there's no extra space requirement.

Mathematics–

Complexity analysis

ApproachTimeSpaceWhen to Use
Direct Reversal ApproachO(d)O(1)When implementing the exact process described or practicing digit manipulation logic.
Optimized Special Case HandlingO(1)O(1)Best solution for interviews and production code; uses the trailing-zero insight instead of full reversal.

Frequently asked questions

Is A Number After a Double Reversal easy or hard?

LeetCode classifies this problem as Easy with an acceptance rate above 80%. The challenge is recognizing the trailing-zero edge case rather than implementing complex algorithms.

A Number After a Double Reversal Python/Java solution

In Python or Java, the optimized solution is a single condition: return num == 0 or num % 10 != 0. A more verbose solution can implement a helper function that reverses digits twice and compares the final value with the original number.

How to solve A Number After a Double Reversal in O(1)?

Use the trailing-zero observation. If a number ends with zero, reversing it removes that zero and the second reversal cannot restore it. Therefore return true if num == 0 or num % 10 != 0. This avoids performing any digit reversal and works in constant time.

What is the best approach for A Number After a Double Reversal?

The optimal approach checks whether the number ends with a zero. If num == 0 or num % 10 != 0, the number remains unchanged after two reversals. Any positive number ending in 0 loses that zero during the first reversal, so the final value differs. This solution runs in O(1) time and O(1) space.

Is A Number After a Double Reversal asked at Google/Amazon/Meta?

The problem represents a common screening-style question focused on number manipulation and edge-case reasoning. Similar math and digit-reversal problems appear in coding interviews at companies like Amazon and Google, especially in early interview rounds.

What data structure is used in A Number After a Double Reversal?

No complex data structure is required. The problem relies purely on integer arithmetic and digit properties. Operations such as modulo (% 10) and integer division (/ 10) are used when implementing the reversal simulation.

What is the time complexity of A Number After a Double Reversal?

The optimal solution runs in O(1) time because it only checks a simple condition on the last digit of the number. A simulation approach that reverses digits twice takes O(d) time, where d is the number of digits, since each digit must be processed during reversal.

Comments

Now write it yourselfClose this page and solve A Number After a Double Reversal in the editor, without looking.
Practise A Number After a Double Reversal