MediumMath

Reverse Integer

Given a signed 32-bit integer x, return x with its digits reversed. If the result falls outside [−2³¹, 2³¹ − 1], return 0. Don't use integers outside the 32-bit range.

x = 1234 → 4321x = −1234 → −4321x = 1234236467 → 0

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

Digits

Each digit moves from the end of x to the end of res

Time and space complexity

|x| has at most 10 digits. The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. x from −2147483648 to 2147483647.