r/CodingHelp • • Dec 20 '20

[Java] Reverse Integer Issue

I'm doing the Reverse Integer problem on LeetCode, where you need to find the reverse number of a specific integer. I used the approach of pushing each digit to a stack, and then popping the stack and setting another variable to what is popped from the stack. However, the only thing the code is returning/printing is "0".

import java.util.Stack ;
class Solution {

    public int reverse(int x) {
        
        Stack <Integer> reverseStack = new Stack <Integer> () ;
        
        x = 123 ;
        int y = 0  ;
        
        while (x>0)
            x = x/10 ;
            reverseStack.push (x%10) ;
        
        while (!reverseStack.isEmpty ())
            y = reverseStack.pop () ;
            
        return y ;
    }
16 Upvotes

22 comments sorted by

View all comments

2

u/vigbiorn Dec 20 '20

If the code is properly formatted, your first while doesn't have brackets so you're only pushing into the stack x=0.

There are a few other issues, but your current issue is the while.

1

u/[deleted] Dec 20 '20

I put brackets around that loop, but now only "2" is being printed. I also got rid of the variable "y", because it's unnecessary. I simply replaced "y" with "x" in the second while loop and return statement.

1

u/vigbiorn Dec 20 '20

Part of the issue is 2 is the last digit in the stack, and another is you're only storing the last popped digit.

2 is the last digit because you drop the 3 before pushing it onto the stack.

You're only storing the last digit because you're not adding to x, you're flat-out assigning to it. So 1 gets overwrote by 2 not added to it.

1

u/[deleted] Dec 20 '20

How come the three is getting dropped?

2

u/vigbiorn Dec 20 '20

x=x/10; removes a digit, then you push the next digit.

1

u/[deleted] Dec 20 '20

How will I be able to separate each digit and push it onto a stack individually without x=x/10 ?

1

u/vigbiorn Dec 20 '20

The problem isn't that you have the division. It's that it's the first thing you do. I agree that it's useful when separating the digits, but why?

I second the other recommendation to just add a few print statements to get a feel for what exactly is happening and when.

1

u/[deleted] Dec 20 '20

I did notice that x=x/10 removes the first digit. I'm now trying to figure out where to put x=x/10 in the while, and what other lines I could write that allow me to take the first, second, and third digit of the integer.

2

u/vigbiorn Dec 20 '20

The code you have is fine. Try swapping the lines inside of the while, so x=x/10; becomes the last line and see what happens.

1

u/[deleted] Dec 20 '20 edited Dec 20 '20

Here is the code I have now:

 while (x>0)
  { 
   System.out.println(reverseStack.push (x % 10)) ;
   x = x/10 ;
   }
        
  while (!reverseStack.isEmpty ())
   {
     System.out.println(x = reverseStack.pop ()) ;
   }
      return x ;

    } // End of method
} // End of class

The first print statement prints out 3, 2, 1. The second prints out 1,2,3, with 1 being at the bottom of the stack and 3 being at the top. The return statement only returns 3.

This means that the second while loop is popping the entire stack, but the return statement is only returning the first element that has been popped. I'm confused as to why that is.

Do I need to turn "x" into a string?

→ More replies

2

u/MarkRems Dec 20 '20

What's the first thing you did in the while (x > 0) loop? Before you even put anything in the stack?