{"id":674,"date":"2025-07-18T13:59:06","date_gmt":"2025-07-18T08:29:06","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=674"},"modified":"2025-07-18T13:59:08","modified_gmt":"2025-07-18T08:29:08","slug":"add-1-to-a-number-represented-as-linked-list","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/","title":{"rendered":"Add 1 to a Linked List Number | GFG Practice | Optimal Solution"},"content":{"rendered":"\n<p>You are given a linked list that represents a number. Each\u00a0node contains a single digit, and the digits\u00a0are stored in the same order as they appear in the number. Add 1 to this number and return the head of the modified linked list.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/add-1-to-a-number-represented-as-linked-list\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>LinkedList: 4->5->6<br><strong>Output: <\/strong>457<br><img fetchpriority=\"high\" decoding=\"async\" width=\"400\" height=\"225\" src=\"https:\/\/media.geeksforgeeks.org\/img-practice\/prod\/addEditProblem\/700053\/Web\/Other\/blobid0_1722278845.png\"><br><strong>Explanation:<\/strong> 4->5->6 represents 456 and when 1 is added it becomes 457. <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>LinkedList: 1-&gt;2-&gt;3\n<strong>Output: <\/strong>124<br><img decoding=\"async\" src=\"https:\/\/media.geeksforgeeks.org\/img-practice\/prod\/addEditProblem\/700053\/Web\/Other\/blobid1_1722278908.png\" width=\"400\" height=\"225\"> <br><strong>Explanation:<\/strong>  1-&gt;2-&gt;3 represents 123 and when 1 is added it becomes 124. <\/pre>\n\n\n\n<p><strong>Expected Time Complexity:&nbsp;<\/strong>O(n)<br><strong>Expected Auxiliary Space:&nbsp;<\/strong>O(1)<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><br>1 &lt;= len(list) &lt;= 10<sup>5<br><\/sup>0 &lt;= list[i] &lt;= 9<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-b9924e65-792c-4203-aff5-9050ce1767cf\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#0-solution-optimal-approach-reverse-and-add\" style=\"\">Solution: Optimal Approach (Reverse and Add)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#8-alternative-approach-without-reversing\" style=\"\">Alternative Approach (Without Reversing)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-1-to-a-number-represented-as-linked-list\/#9-summary\" style=\"\">Summary<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-solution-optimal-approach-reverse-and-add\">Solution: Optimal Approach (Reverse and Add)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like adding 1 to a number written on paper! When we add 1 to a number, we start from the rightmost digit (least significant). If that digit is 9, it becomes 0 and we carry 1 to the next digit. We keep doing this until we don&#8217;t have a carry anymore. Since our linked list starts from the leftmost digit, we need to reverse it first, do our addition, and then reverse it back!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Reverse the List<\/strong>: Since we need to add from the least significant digit, reverse the entire linked list<\/li>\n\n\n\n<li><strong>Add One<\/strong>: Start from the new head (which was the last digit originally) and add 1<\/li>\n\n\n\n<li><strong>Handle Carry<\/strong>: If the current digit is 9, it becomes 0 and we carry 1 to the next digit<\/li>\n\n\n\n<li><strong>Continue with Carry<\/strong>: Keep propagating the carry until we find a digit that&#8217;s not 9<\/li>\n\n\n\n<li><strong>Handle New Digit<\/strong>: If we reach the end with a carry, create a new node with value 1<\/li>\n\n\n\n<li><strong>Reverse Back<\/strong>: Reverse the list again to restore the original order<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def reverse(self, head):\n        temp = head\n        prev = None\n        while temp is not None:\n            front = temp.next      # Store next node before breaking link\n            temp.next = prev       # Reverse the link\n            prev = temp            # Move prev forward\n            temp = front           # Move temp forward\n        return prev               # Return new head of reversed list\n\n    def addOne(self, head):\n        # Step 1: Reverse the linked list to process from least significant digit\n        new_head = self.reverse(head)\n        temp = new_head\n        carry = 0\n        \n        # Step 2: Add 1 and handle carry propagation\n        while temp:\n            if temp.data == 9:\n                carry = 1         # Set carry for next digit\n                temp.data = 0     # Current digit becomes 0\n            else:\n                temp.data = temp.data + 1  # Simply add 1 to current digit\n                break             # No more carry needed, we're done\n            \n            # Step 3: Handle case where we need to add new digit at front\n            if temp.next is None:\n                if carry == 1:\n                    temp.next = Node(1)  # Add new node with value 1\n                    break\n            temp = temp.next\n        \n        # Step 4: Reverse the list back to original order\n        return self.reverse(new_head)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">reverse<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp = head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        prev = <\/span><span style=\"color: #569CD6\">None<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> temp <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            front = temp.next      <\/span><span style=\"color: #6A9955\"># Store next node before breaking link<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp.next = prev       <\/span><span style=\"color: #6A9955\"># Reverse the link<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            prev = temp            <\/span><span style=\"color: #6A9955\"># Move prev forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = front           <\/span><span style=\"color: #6A9955\"># Move temp forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> prev               <\/span><span style=\"color: #6A9955\"># Return new head of reversed list<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">addOne<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Step 1: Reverse the linked list to process from least significant digit<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        new_head = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.reverse(head)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp = new_head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        carry = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Step 2: Add 1 and handle carry propagation<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> temp:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> temp.data == <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                carry = <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">         <\/span><span style=\"color: #6A9955\"># Set carry for next digit<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                temp.data = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">     <\/span><span style=\"color: #6A9955\"># Current digit becomes 0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                temp.data = temp.data + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Simply add 1 to current digit<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">break<\/span><span style=\"color: #D4D4D4\">             <\/span><span style=\"color: #6A9955\"># No more carry needed, we&#39;re done<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Step 3: Handle case where we need to add new digit at front<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> temp.next <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> carry == <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    temp.next = Node(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)  <\/span><span style=\"color: #6A9955\"># Add new node with value 1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #C586C0\">break<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = temp.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Step 4: Reverse the list back to original order<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.reverse(new_head)<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>This solution works in three main phases. First, we reverse the entire linked list so we can process digits from right to left (least to most significant). Then we add 1 starting from the new head. If the current digit is 9, it becomes 0 and we set a carry for the next digit. If it&#8217;s not 9, we simply add 1 and we&#8217;re done. The tricky part is handling the case where we have a carry even after processing all digits (like 999 \u2192 1000) &#8211; in this case, we create a new node. Finally, we reverse the list back to its original order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>9 -&gt; 9 -&gt; 9<\/code>&nbsp;(represents 999)<\/p>\n\n\n\n<p><strong>Step 1: Reverse the list<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Original:\u00a0<code>9 -> 9 -> 9<\/code><\/li>\n\n\n\n<li>Reversed:\u00a0<code>9 -> 9 -> 9<\/code>\u00a0(same in this case)<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2: Add 1 and handle carry<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start at first 9: data = 9, set carry = 1, data becomes 0<\/li>\n\n\n\n<li>Move to second 9: data = 9, set carry = 1, data becomes 0<\/li>\n\n\n\n<li>Move to third 9: data = 9, set carry = 1, data becomes 0<\/li>\n\n\n\n<li>Reached end with carry = 1, create new node:\u00a0<code>0 -> 0 -> 0 -> 1<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3: Reverse back<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Current:\u00a0<code>0 -> 0 -> 0 -> 1<\/code><\/li>\n\n\n\n<li>Reversed:\u00a0<code>1 -> 0 -> 0 -> 0<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>1 -&gt; 0 -&gt; 0 -&gt; 0<\/code>&nbsp;(represents 1000)<\/p>\n\n\n\n<p>Let&#8217;s also trace:&nbsp;<code>1 -&gt; 2 -&gt; 3<\/code>&nbsp;(represents 123)<\/p>\n\n\n\n<p><strong>Step 1: Reverse the list<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Original:\u00a0<code>1 -> 2 -> 3<\/code><\/li>\n\n\n\n<li>Reversed:\u00a0<code>3 -> 2 -> 1<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2: Add 1<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start at 3: data = 3 (not 9), so data becomes 4, break<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3: Reverse back<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Current:\u00a0<code>4 -> 2 -> 1<\/code><\/li>\n\n\n\n<li>Reversed:\u00a0<code>1 -> 2 -> 4<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>1 -&gt; 2 -&gt; 4<\/code>&nbsp;(represents 124)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n) &#8211; We traverse the list three times (two reversals + one addition), but it&#8217;s still linear<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1) &#8211; We only use a few pointer variables, no extra space needed<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is like doing manual addition on paper, but since the number is written left-to-right and we need to add from right-to-left, we first flip the paper, do our addition, and then flip it back. The key insight is that we need to access the least significant digit first, which requires reversing the list.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-alternative-approach-without-reversing\">Alternative Approach (Without Reversing)<\/h2>\n\n\n\n<p>While the reverse approach is optimal, there&#8217;s also a recursive solution that doesn&#8217;t require reversing:<\/p>\n\n\n\n<p><strong>Concept<\/strong>: Use recursion to reach the end of the list first, then add 1 and propagate the carry back up through the recursive calls.<\/p>\n\n\n\n<p><strong>Intuition<\/strong>: It&#8217;s like having a chain of people passing a message. The last person adds 1 and passes any carry back to the previous person.<\/p>\n\n\n\n<p>However, this approach has O(n) space complexity due to the recursion stack, making the reverse approach more space-efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-summary\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Approach<\/th><th>Time<\/th><th>Space<\/th><th>Difficulty<\/th><th>Best For<\/th><\/tr><\/thead><tbody><tr><td>Reverse and Add<\/td><td>O(n)<\/td><td>O(1)<\/td><td>Medium<\/td><td>Production code<\/td><\/tr><tr><td>Recursive<\/td><td>O(n)<\/td><td>O(n)<\/td><td>Medium<\/td><td>Understanding recursion<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The&nbsp;<strong>reverse and add approach<\/strong>&nbsp;is generally preferred because it uses constant space while maintaining linear time complexity. The key insight is that addition naturally works from right to left, so we reverse the list to make the rightmost digit accessible first.<\/p>\n\n\n\n<p>This problem beautifully demonstrates how understanding the fundamental nature of an operation (addition works right to left) can lead to an elegant solution by adapting the data structure (reversing the list) to match the operation&#8217;s requirements!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a linked list that represents a number. Each\u00a0node contains a single digit, and the digits\u00a0are stored in the same order as they appear in the number. Add 1 to this number and return the head of the modified linked list. Here&#8217;s the [Problem Link] to begin with. Input: LinkedList: 4->5->6Output: 457Explanation: 4->5->6<\/p>\n","protected":false},"author":1,"featured_media":676,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[19,29],"class_list":{"0":"post-674","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-medium","10":"tag-singly-linked-list"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/add-1-to-a-linked-list-number-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions\/677"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/676"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}