{"id":678,"date":"2025-07-18T14:05:38","date_gmt":"2025-07-18T08:35:38","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=678"},"modified":"2025-07-18T14:05:39","modified_gmt":"2025-07-18T08:35:39","slug":"add-two-numbers","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/","title":{"rendered":"Add Two Numbers | Leetcode 2 | Optimal Solution"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>non-empty<\/strong>&nbsp;linked lists representing two non-negative integers. The digits are stored in&nbsp;<strong>reverse order<\/strong>, and each of their nodes contains a single digit. Add the two numbers and return the sum&nbsp;as a linked list.<\/p>\n\n\n\n<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/add-two-numbers\/description\/\" 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<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/02\/addtwonumber1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> l1 = [2,4,3], l2 = [5,6,4]<br><strong>Output:<\/strong> [7,0,8]<br><strong>Explanation:<\/strong> 342 + 465 = 807.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> l1 = [0], l2 = [0]<br><strong>Output:<\/strong> [0]<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]<br><strong>Output:<\/strong> [8,9,9,9,0,0,0,1]<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The number of nodes in each linked list is in the range\u00a0<code>[1, 100]<\/code>.<\/li>\n\n\n\n<li><code>0 &lt;= Node.val &lt;= 9<\/code><\/li>\n\n\n\n<li>It is guaranteed that the list represents a number that does not have leading zeros.<\/li>\n<\/ul>\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-ff26306a-6355-456b-9c89-fe3b9eb1379a\" 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-two-numbers\/#0-solution-optimal-approach-digit-by-digit-addition\" style=\"\">Solution: Optimal Approach (Digit by Digit Addition)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/#8-key-insights\" style=\"\">Key Insights<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/add-two-numbers\/#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-digit-by-digit-addition\">Solution: Optimal Approach (Digit by Digit Addition)<\/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 two numbers on paper, but instead of writing digits left-to-right, we&#8217;re adding them right-to-left (which is actually easier!). Since the linked lists already store digits in reverse order, we can directly add corresponding digits from both lists, handle any carry, and build our result list as we go. It&#8217;s like having two people read out digits from the end of two numbers, and you write down the sum digit by digit!<\/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>Create Result Structure<\/strong>: Use a dummy node to simplify the result list construction<\/li>\n\n\n\n<li><strong>Initialize Variables<\/strong>: Keep track of carry and current position in result list<\/li>\n\n\n\n<li><strong>Process Both Lists<\/strong>: While either list has nodes or we have a carry to process<\/li>\n\n\n\n<li><strong>Add Digits<\/strong>: Sum the current digits from both lists plus any carry<\/li>\n\n\n\n<li><strong>Handle Carry<\/strong>: If sum is greater than 9, set carry for next iteration<\/li>\n\n\n\n<li><strong>Create Result Node<\/strong>: Add the current digit (sum % 10) to result list<\/li>\n\n\n\n<li><strong>Move Forward<\/strong>: Advance pointers in both input lists and result list<\/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 addTwoNumbers(\n        self, l1: Optional[ListNode], l2: Optional[ListNode]\n    ) -&gt; Optional[ListNode]:\n        result = ListNode(0)     # Dummy node to simplify list construction\n        temp = result            # Pointer to build result list\n        carry = 0               # Variable to handle carry from previous addition\n        \n        # Continue while either list has nodes or we have carry\n        while (l1 != None and l2 != None) or carry:\n            total = 0           # Sum of current digits plus carry\n            \n            # Add digit from first list if available\n            if l1 != None:\n                total += l1.val\n                l1 = l1.next    # Move to next node\n            \n            # Add digit from second list if available\n            if l2 != None:\n                total += l2.val\n                l2 = l2.next    # Move to next node\n            \n            total += carry      # Add carry from previous iteration\n            \n            # Calculate carry for next iteration\n            if total &gt; 9:\n                carry = 1\n            else:\n                carry = 0\n            \n            # Create new node with current digit and add to result\n            temp.next = ListNode(total % 10)\n            temp = temp.next    # Move result pointer forward\n        \n        return result.next      # Return actual result (skip dummy node)\" 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\">addTwoNumbers<\/span><span style=\"color: #D4D4D4\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">l1<\/span><span style=\"color: #D4D4D4\">: Optional[ListNode], <\/span><span style=\"color: #9CDCFE\">l2<\/span><span style=\"color: #D4D4D4\">: Optional[ListNode]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    ) -&gt; Optional[ListNode]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result = ListNode(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">)     <\/span><span style=\"color: #6A9955\"># Dummy node to simplify list construction<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp = result            <\/span><span style=\"color: #6A9955\"># Pointer to build result list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        carry = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">               <\/span><span style=\"color: #6A9955\"># Variable to handle carry from previous addition<\/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\"># Continue while either list has nodes or we have carry<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> (l1 != <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> l2 != <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">) <\/span><span style=\"color: #569CD6\">or<\/span><span style=\"color: #D4D4D4\"> carry:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            total = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">           <\/span><span style=\"color: #6A9955\"># Sum of current digits plus carry<\/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\"># Add digit from first list if available<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> l1 != <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                total += l1.val<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                l1 = l1.next    <\/span><span style=\"color: #6A9955\"># Move to next node<\/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\"># Add digit from second list if available<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> l2 != <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                total += l2.val<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                l2 = l2.next    <\/span><span style=\"color: #6A9955\"># Move to next node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            total += carry      <\/span><span style=\"color: #6A9955\"># Add carry from previous iteration<\/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\"># Calculate carry for next iteration<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> total &gt; <\/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>\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\">                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\"># Create new node with current digit and add to result<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp.next = ListNode(total % <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = temp.next    <\/span><span style=\"color: #6A9955\"># Move result pointer forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result.next      <\/span><span style=\"color: #6A9955\"># Return actual result (skip dummy node)<\/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 simulates the manual addition process we learned in school. We use a dummy node to make list construction easier &#8211; this way we don&#8217;t need to handle the special case of creating the first node. We process both lists simultaneously, adding corresponding digits along with any carry from the previous addition. The key insight is that we continue the loop even after one or both lists are exhausted, as long as we have a carry to process. We use modulo arithmetic to get the current digit (total % 10) and integer division logic to determine the carry.<\/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>l1 =&nbsp;<\/code>(represents 342),&nbsp;<code>l2 =&nbsp;<\/code>(represents 465)<\/p>\n\n\n\n<p><strong>Initial Setup:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>result = dummy node, temp = result, carry = 0<\/li>\n\n\n\n<li>l1 points to 2, l2 points to 5<\/li>\n<\/ul>\n\n\n\n<p><strong>Iteration 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>total = 2 + 5 + 0 = 7<\/li>\n\n\n\n<li>carry = 0 (since 7 \u2264 9)<\/li>\n\n\n\n<li>Create node with value 7: result -> 7<\/li>\n\n\n\n<li>Move: l1 to 4, l2 to 6, temp to node 7<\/li>\n<\/ul>\n\n\n\n<p><strong>Iteration 2:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>total = 4 + 6 + 0 = 10<\/li>\n\n\n\n<li>carry = 1 (since 10 > 9)<\/li>\n\n\n\n<li>Create node with value 0: result -> 7 -> 0<\/li>\n\n\n\n<li>Move: l1 to 3, l2 to 4, temp to node 0<\/li>\n<\/ul>\n\n\n\n<p><strong>Iteration 3:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>total = 3 + 4 + 1 = 8<\/li>\n\n\n\n<li>carry = 0 (since 8 \u2264 9)<\/li>\n\n\n\n<li>Create node with value 8: result -> 7 -> 0 -> 8<\/li>\n\n\n\n<li>Move: l1 to None, l2 to None, temp to node 8<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Check:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Both lists exhausted and carry = 0, so loop ends<\/li>\n\n\n\n<li>Return result.next:<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;&#8220; represents 807, which is 342 + 465 \u2713<\/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(max(m, n)) &#8211; We process each digit from both lists once, where m and n are the lengths of the lists<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(max(m, n)) &#8211; The result list will have at most max(m, n) + 1 nodes<\/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 being a calculator that works digit by digit from right to left. You look at the rightmost digits of both numbers, add them up (plus any carry from before), write down the result digit, and remember if you need to carry anything to the next position. Since the linked lists are already in reverse order, this process becomes very natural and straightforward.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-key-insights\">Key Insights<\/h3>\n\n\n\n<p><strong>Why the dummy node helps:<\/strong><br>Using a dummy node eliminates the need to handle the special case of creating the first result node. We can always do&nbsp;<code>temp.next = new_node<\/code>&nbsp;instead of checking if we&#8217;re creating the first node.<\/p>\n\n\n\n<p><strong>Why we continue with carry:<\/strong><br>Even when both input lists are exhausted, we might still have a carry to process. For example, adding 99 + 1 would require adding an extra digit at the front.<\/p>\n\n\n\n<p><strong>Why modulo arithmetic works:<\/strong><br><code>total % 10<\/code>&nbsp;gives us the digit to store in the current position, while&nbsp;<code>total &gt; 9<\/code>&nbsp;tells us if we need to carry 1 to the next position.<\/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>Digit by Digit Addition<\/td><td>O(max(m,n))<\/td><td>O(max(m,n))<\/td><td>Medium<\/td><td>Production code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This&nbsp;<strong>optimal solution<\/strong>&nbsp;efficiently handles the addition in a single pass while properly managing carries. The key insight is that since the digits are already in reverse order, we can directly simulate the manual addition process without any preprocessing.<\/p>\n\n\n\n<p>This problem beautifully demonstrates how understanding the problem&#8217;s natural structure (digits in reverse order) can lead to an elegant solution. The algorithm is both intuitive and efficient, making it a great example of how to handle linked list manipulation with mathematical operations!<\/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 two&nbsp;non-empty&nbsp;linked lists representing two non-negative integers. The digits are stored in&nbsp;reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum&nbsp;as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Here&#8217;s the [Problem<\/p>\n","protected":false},"author":1,"featured_media":679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[19,29],"class_list":{"0":"post-678","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-two-numbers-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\/678","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=678"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"predecessor-version":[{"id":680,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/678\/revisions\/680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/679"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}