diff --git a/BitManipulation/Reverse Integer.java b/BitManipulation/Reverse Integer.java new file mode 100644 index 0000000..f467e63 --- /dev/null +++ b/BitManipulation/Reverse Integer.java @@ -0,0 +1,19 @@ +class Solution { + + public int reverse(int x) { + boolean isNegative = x < 0; + + x = Math.abs(x); + + int num = 0; + + while (x > 0) { + if (Integer.MAX_VALUE / 10 < num) return 0; + + num = 10 * num + x % 10; + x /= 10; + } + + return isNegative ? -num : num; + } +}