{"id":3154,"date":"2018-07-14T21:48:51","date_gmt":"2018-07-15T04:48:51","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3154"},"modified":"2021-04-07T23:55:17","modified_gmt":"2021-04-08T06:55:17","slug":"leetcode-445-add-two-numbers-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-445-add-two-numbers-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 445. Add Two Numbers II"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>You are given two\u00a0<b>non-empty<\/b>\u00a0linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.<\/p>\n<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.<\/p>\n<p><b>Follow up:<\/b><br \/>\nWhat if you cannot modify the input lists? In other words, reversing the lists is not allowed.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> (7 -&gt; 2 -&gt; 4 -&gt; 3) + (5 -&gt; 6 -&gt; 4)\n<b>Output:<\/b> 7 -&gt; 8 -&gt; 0 -&gt; 7<\/pre>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Using a stack to &#8220;reverse&#8221; the list. Simulate the addition digit by digit.<\/p>\n<p>Time complexity: O(l1 + l2)<\/p>\n<p>Space complexity: O(l1 + l2)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\n\/\/ Running time: 40 ms\nclass Solution {\npublic:\n  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\n    stack&lt;int&gt; s1;\n    stack&lt;int&gt; s2;\n    while (l1) {\n      s1.push(l1-&gt;val);\n      l1 = l1-&gt;next;\n    }    \n    while (l2) {\n      s2.push(l2-&gt;val);\n      l2 = l2-&gt;next;\n    }    \n    ListNode* head = nullptr;\n    int sum = 0;\n    while (!s1.empty() || !s2.empty() || sum) {\n      sum += s1.empty() ? 0 : s1.top();\n      sum += s2.empty() ? 0 : s2.top();\n      if (!s1.empty()) s1.pop();\n      if (!s2.empty()) s2.pop();            \n      ListNode* n = new ListNode(sum % 10);\n      sum \/= 10;\n      n-&gt;next = head;\n      head = n;      \n    }    \n    return head;      \n  }\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-2-add-two-numbers\/\">\u82b1\u82b1\u9171 LeetCode 2. Add Two Numbers<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem You are given two\u00a0non-empty\u00a0linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[332,83,177,179,180],"class_list":["post-3154","post","type-post","status-publish","format-standard","hentry","category-list","tag-add","tag-list","tag-medium","tag-simulation","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3154"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3154\/revisions"}],"predecessor-version":[{"id":8334,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3154\/revisions\/8334"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}