class Solution {
public:
int reverse(long x) {
string s = to_string(x);
string f = "";
for(int i=s.length()-1;i>=0;i--) // looping backwards through string
{
if(s[i] != '-') f += s[i]; // reversing the string but not the negative if there is one
}
if(x < 0) f = '-'+f; // adding the negative if needed
int64_t v;
istringstream(f) >> v; // turns string into int
if(v > INT_MAX - 1 || v < -INT_MAX) return 0; // just in case if number is over 32 bit
return v;
}
};