> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/7_reverse_integer.md).

# 7 Reverse Integer

Reverse digits of an integer.

```
Example1: x = 123, return 321
Example2: x = -123, return -321
```

```cpp
int reverse(int x) {
    bool notsign = x > 0;

    x = abs(x);

    int result = 0;
    while (x > 0) {
        if (result > INT_MAX / 10) return 0;
        result = result * 10 + x % 10;
        x /= 10;
    }

    if (notsign) {
        return result;
    }
    return result * -1;
}
```
