Back to Practice Dashboard
Python BasicsMedium

Reversing a Number using Recursion

Learn how to solve the 'Reversing a Number using Recursion' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Medium

Write a function reverse_number(n) that reverses the digits of a given non-negative integer n using recursion and returns the reversed number as an integer. For example, 1234 becomes 4321. Leading zeros in the reversed result should be dropped (e.g., 1200 reversed is 21).

Constraints
  • 0 <= n <= 10^9

Examples

Example 1
Input
n = 1234
Output
4321
Explanation

The digits 1, 2, 3, 4 are reversed to 4, 3, 2, 1 giving 4321.

Example 2
Input
n = 1200
Output
21
Explanation

Reversed digits are 0, 0, 2, 1. Leading zeros are dropped, giving 21.

Example 3
Input
n = 5
Output
5
Explanation

A single digit number reversed is itself.

Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor