Reverse Integer Easy 0 attempts
LeetCode ↗

Reverse Integer

Easy MatrixBFSDFS LeetCode

Given a signed 32-bit integer x, return x with its digits reversed. Return 0 if the result overflows.

Example: x = 123 → 321, x = -123 → -321

Sample Input
Sample Output
Constraints
  • -2^31 <= x <= 2^31 - 1
Test Cases
Case 1
Args: [123] Expected: 321
Case 2
Args: [-123] Expected: -321
Case 3
Args: [120] Expected: 21
Topics

Pop and Push Digits

function reverse(x) {
  let result = 0;
  while (x !== 0) {
    result = result * 10 + x % 10;
    x = Math.trunc(x / 10);
  }
  return result > 2**31 - 1 || result < -(2**31) ? 0 : result;
}

Time: O(log x) | Space: O(1)

Saved in this browser only. Private to you.

JavaScript