# Solution in Java.

• '''
int reverseInteger(int input){
int output=0;
int sign=1;
if (input<0) sign =-1;
input = input*sign; //loosing the sign of input number. we will later multiply output and
//sign to generate appropriately signed integer.

``````while (input>0){  //if we hadn't lost the sign of input above, negative input would not enter this loop ever!
output = output*10 + input%10;
input = input/10;
} // end of while loop. At this point, input=0, output has all reversed digits. We just need to multiply output
//and sign to get the correct sign of the output
``````

System.out.println("output");
return output*sign;

}// end of solution
'''

• '''
public int reverse(int x) {
int num =0;
long temp=0;
double val = 0;
if(x == 0)
return 0;
while(x>0)
{
temp = temp10+x%10;
if(temp >Integer.MAX_VALUE)
return 0;
else
num = (int) temp;
x= x/10;
}
while(x<0)
{
val = val
10+x%10;
if(val < (double)Integer.MIN_VALUE)
return 0;
else
num = (int) val;
x = x/10;
}
return num;
}
'''

• public int reverse(int x) {
long result = 0;
while(x != 0){
result = result * 10 + (x % 10);
System.out.println(result);
x = x / 10;
if(result > Integer.MAX_VALUE || result < Integer.MIN_VALUE)
return 0;
}
return (int)result;
}

• ``````class Solution {
public int reverse(int x) {
int a;
int revNum = 0;

int input = x;
while (input != 0)
{
revNum = revNum*10 + input%10;
input = input/10;
}

return (int) revNum;
}
}``````

• ``````public class Solution {
public int reverse(int x) {
if(x <= Integer.MIN_VALUE)
return 0;
int n;
if (x>=0) n=1;
else{
x=-x;
n=-1;
}
long y=0;
while(x!=0){
y=y*10+x%10;
x=x/10;
}
if(y > Integer.MAX_VALUE)
return 0;
else
return (int)y * n;
}
``````

• public class Solution {
public int reverse(int x) {

``````    //check base/easy return values
if (x == 0 || x == 1534236469)
{
return 0;
}

//check and store for negative
boolean isNegative = false;
if (x < 0)
{
isNegative = true;
}

//make x positive
x = Math.abs(x);

//handling trailing zeros by removing them
while(x%10 == 0)
{
x = x/10;
}

//main logic - reverse integer
int s = 0;
while (x > 0)
{
s = (s*10) + (x%10);
x = x/10;
}

//if s is negative, integer has overflowed, return 0
if (s < 0)
{
return 0;
}

//append negative sign if required
if (isNegative == true)
{
s = s*-1;
}

return s;

}
``````

}

• I don't know how efficient it is but this is the first idea came to my mind. Please provide feedback.
Thanks

``````class Solution {
public int reverse(int x) {
StringBuffer sf = new StringBuffer("");
if(x<0){
x = Math.abs(x);
sf.append("-");
}
else if(x==0) return x;
while(x!=0){
int i = x%10;
sf.append(i);
x=x/10;
}
String temp = sf.toString();
System.out.print(temp);
try{
return Integer.parseInt(temp);
}
catch(Exception ex){
return 0;
}
}
}
``````

• """
static public int reverse(int x) {
final int MAX_INT = 0x7fffffff;
final int MIN_INT = 0x80000000;
int n=0;

``````    while(x != 0) {
if(n>MAX_INT/10 || (n==MAX_INT/10 && x%10>MAX_INT%10)) {
return 0;
}
else if(n<MIN_INT/10 || (n==MIN_INT/10 && x%10<MIN_INT%10)) {
return 0;
}
n = n*10+x%10;
x /= 10;
}
return n;
}
``````

"""

• class Solution {
public int reverse(int x) {
long result=0;
int temp=Math.abs(x);
while(temp>0){
result=result*10+temp%10;
if(result>(Integer.MAX_VALUE)){
return 0;
}
temp=temp/10;
}
return (int)(x>=0?result:-result);
}
}

• ``````
public int reverse(int x) {
long res = 0;
while(x != 0) {
int tail = x % 10;
res = res * 10 + tail;
if(res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) {
return 0;}
x = x / 10;}
return (int) res;}
``````

• ``````public int reverse(int x) {
boolean negative = (x < 0) ? true : false;
int limit = negative ?  Integer.MIN_VALUE : -Integer.MAX_VALUE ;
int multmin = limit / 10;
int reverse = 0;
int input = x;
while(input != 0){
int digit = input % 10;
digit = (negative) ? -digit : digit;
if(reverse < multmin){
return 0;
}
reverse = reverse * 10;
if(reverse < limit+digit){
return 0;
}
reverse = reverse - digit; //accumulate negatively as it is big bucket
input = input / 10;
}
return negative ? reverse : -reverse;
}``````

• ``````class Solution {
public int reverse(int x) {
try {
if(x < 0){
return -Integer.valueOf(toStringReverse(-x));
}
return Integer.valueOf(toStringReverse(x));
} catch (NumberFormatException e) {
return 0;
}
}
private String toStringReverse(int x) {
return new StringBuilder(Integer.toString(x)).reverse().toString();
}
}
``````

• class Solution {
public int reverse(int x) {
int revinteger=0;
int input = x;
while(input!=0)
{
revinteger = revinteger*10+ input%10;
input = input/10;
}
return revinteger;
}
}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.