AlgoInsight

DSA problem

Reverse Digits of a Number

EasyMathPattern: MathbeginnerFree

Given an integer N, reverse the order of its digits and return the result. For example, 1234 becomes 4321 and -1234 becomes -4321.

Use the same digit-extraction technique as the palindrome problem: pull off the last digit with N % 10, append it with result = result * 10 + digit, then remove it with integer division, repeating until N reaches 0. Handle negative numbers by reversing the absolute value and reattaching the sign.

A subtle follow-up is overflow: in fixed-width integer languages the reversed value can exceed the type's range, so a defensive solution checks before multiplying (or uses a wider accumulator).

Examples

Example 1
Input: N = -1234
Output: -4321
Explanation: Reversing the digits of 1234 gives 4321, and the original sign is reattached.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

math

Browse all DSA problems