{"id":3293,"date":"2018-07-25T23:43:48","date_gmt":"2018-07-26T06:43:48","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3293"},"modified":"2018-09-11T21:11:39","modified_gmt":"2018-09-12T04:11:39","slug":"leetcode-2-add-two-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-2-add-two-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2. Add Two Numbers"},"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 digits are stored in\u00a0<b>reverse order<\/b>\u00a0and 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>Example<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> (2 -&gt; 4 -&gt; 3) + (5 -&gt; 6 -&gt; 4)\r\n<b>Output:<\/b> 7 -&gt; 0 -&gt; 8\r\n<b>Explanation:<\/b> 342 + 465 = 807.<\/pre>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Simulate the addition, draw two numbers (one each) from l1, l2, if list is empty draw 0.<\/p>\n<p>Using a dummy head makes things easier.<\/p>\n<p>Time complexity: O(max(l1,l2))<\/p>\n<p>Space complexity: O(max(l1,l2))<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 28 ms\r\nclass Solution {\r\npublic:\r\n  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\r\n    ListNode dummy(0);\r\n    ListNode* tail = &amp;dummy;\r\n    int sum = 0;\r\n    while (l1 || l2 || sum) {\r\n      sum += (l1 ? l1-&gt;val : 0) + (l2 ? l2-&gt;val : 0);\r\n      l1 = l1 ? l1-&gt;next : nullptr;\r\n      l2 = l2 ? l2-&gt;next : nullptr;\r\n      tail-&gt;next = new ListNode(sum % 10);\r\n      sum \/= 10;\r\n      tail = tail-&gt;next;\r\n    }            \r\n    return dummy.next;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 30 ms\r\nclass Solution {\r\n  public ListNode addTwoNumbers(ListNode l1, ListNode l2) {\r\n    int sum = 0;\r\n    ListNode dummy = new ListNode(0);\r\n    ListNode tail = dummy;\r\n    while (l1 != null || l2 != null || sum != 0) {\r\n      if (l1 != null) {\r\n        sum += l1.val;\r\n        l1 = l1.next;\r\n      }\r\n      if (l2 != null) {\r\n        sum += l2.val;\r\n        l2 = l2.next;\r\n      }\r\n      tail.next = new ListNode(sum % 10);\r\n      sum \/= 10;\r\n      tail = tail.next;\r\n    }\r\n    return dummy.next;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 112 ms\r\n\"\"\"\r\nclass Solution:\r\n  def addTwoNumbers(self, l1, l2):\r\n    s = 0\r\n    dummy = ListNode(0)\r\n    tail = dummy\r\n    while l1 or l2 or s &gt; 0:      \r\n      s += (l1.val if l1 else 0) + (l2.val if l2 else 0)\r\n      l1 = l1.next if l1 else None\r\n      l2 = l2.next if l2 else None\r\n      tail.next = ListNode(s % 10)\r\n      s \/\/= 10\r\n      tail = tail.next\r\n    return dummy.next<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-445-add-two-numbers-ii\/\">\u82b1\u82b1\u9171 LeetCode 445. Add Two Numbers II<\/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 digits are stored in\u00a0reverse order\u00a0and each of their nodes contain a single digit. Add&#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":[83,177,179],"class_list":["post-3293","post","type-post","status-publish","format-standard","hentry","category-list","tag-list","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3293","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=3293"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3293\/revisions"}],"predecessor-version":[{"id":3910,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3293\/revisions\/3910"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}