<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Leetcoding with Rithikesh]]></title><description><![CDATA[Leetcoding with Rithikesh]]></description><link>https://leetcoder-rithikesh.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 03:32:53 GMT</lastBuildDate><atom:link href="https://leetcoder-rithikesh.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[7. Reverse Integer]]></title><description><![CDATA[Problem statement:
Link to question
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2**31, 2**31 - 1], then return 0.
Assume the environment does not ...]]></description><link>https://leetcoder-rithikesh.hashnode.dev/reverse-integer</link><guid isPermaLink="true">https://leetcoder-rithikesh.hashnode.dev/reverse-integer</guid><category><![CDATA[DSA]]></category><category><![CDATA[leetcode]]></category><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[Rithikesh Surthi]]></dc:creator><pubDate>Sun, 29 Jan 2023 17:16:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1675011319260/c730d696-59bd-4e0e-8ba5-cfc15385f0e8.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-problem-statement">Problem statement:</h1>
<p><a target="_blank" href="https://leetcode.com/problems/reverse-integer/">Link to question</a></p>
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code> <em>with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2**31, 2**31 - 1]</code>, then return <code>0</code>.</p>
<p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p>
<p><strong>Example 1:</strong></p>
<pre><code class="lang-cpp">Input: x = <span class="hljs-number">123</span>
Output: <span class="hljs-number">321</span>
</code></pre>
<p><strong>Example 2:</strong></p>
<pre><code class="lang-c">Input: x = <span class="hljs-number">-123</span>
Output: <span class="hljs-number">-321</span>
</code></pre>
<p><strong>Example 3:</strong></p>
<pre><code class="lang-cpp">Input: x = <span class="hljs-number">120</span>
Output: <span class="hljs-number">21</span>
</code></pre>
<h1 id="heading-code">Code:</h1>
<pre><code class="lang-cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> {</span>
<span class="hljs-keyword">public</span>:
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">reverse</span><span class="hljs-params">(<span class="hljs-keyword">int</span> N)</span> </span>{
        <span class="hljs-keyword">long</span> rev_num = <span class="hljs-number">0</span>, digit, temp;

        <span class="hljs-keyword">while</span>(N){
            digit = N % <span class="hljs-number">10</span>;
            <span class="hljs-comment">//Extracting unit digit and joining from the end by multiplying with 10.</span>
            temp = (rev_num * <span class="hljs-number">10</span>) + digit;
            <span class="hljs-keyword">if</span> (temp / <span class="hljs-number">10</span> != rev_num)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
            <span class="hljs-keyword">if</span> ((rev_num &gt; <span class="hljs-number">0</span> &amp;&amp; rev_num &gt; (INT_MAX - digit)/<span class="hljs-number">10</span>) || (rev_num &lt; <span class="hljs-number">0</span> &amp;&amp; rev_num &lt; (INT_MIN - digit)/<span class="hljs-number">10</span>)) 
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
            rev_num = temp;
            N /= <span class="hljs-number">10</span>;
        }
        <span class="hljs-keyword">return</span> rev_num;
    }
};
</code></pre>
<h2 id="heading-explanation">Explanation:</h2>
<p>Here is a detailed explanation of each step:</p>
<ol>
<li><p>Initialize a long-type variable <code>rev_num</code> to store the reversed integer.</p>
</li>
<li><p>The while loop continues as long as <code>N</code> is not 0.</p>
</li>
<li><p>In each iteration, the last digit of <code>N</code> is extracted using the modulo operator <code>% 10</code>.</p>
</li>
<li><p>The extracted digit is added to the <code>rev_num</code> by multiplying it by 10 and then adding the extracted digit. This is done by first creating a temporary variable <code>temp</code> equal to <code>rev_num * 10 + N % 10</code>.</p>
</li>
<li><p>The code checks for overflow by comparing <code>temp / 10</code> with <code>rev_num</code>. If they are not equal, it means the reversed integer has overflowed the range of the int type, and the function returns 0.</p>
</li>
<li><p>If there is no overflow, <code>rev_num</code> is updated to <code>temp</code> and <code>N</code> is updated to <code>N / 10</code> to remove the last digit.</p>
</li>
<li><p>The while loop continues until <code>N</code> becomes 0.</p>
</li>
<li><p>Finally, the reversed integer is returned as the result of the function.</p>
</li>
</ol>
<p><strong>Note:</strong> It's important to use a long type variable to store the reversed integer as the int type may overflow if the input integer is very large.</p>
<h2 id="heading-summary">Summary:</h2>
<p>The code uses a while loop to extract the last digit of the input integer <code>N</code> by using the modulo operator <code>% 10</code> and adds it to the result <code>rev_num</code> after multiplying by 10. It checks for overflow by comparing <code>temp / 10</code> with <code>rev_num</code> and returns 0 if they are not equal. After each iteration, <code>N</code> is updated to <code>N / 10</code> to remove the last digit.</p>
<h3 id="heading-thank-you-for-taking-the-time-to-read-my-article-i-look-forward-to-continuing-to-share-my-writing-with-you-and-hearing-more-of-your-thoughts-and-opinions">Thank you for taking the time to read my article. I look forward to continuing to share my writing with you and hearing more of your thoughts and opinions.</h3>
<h3 id="heading-happy-learning">Happy learning!</h3>
]]></content:encoded></item></channel></rss>