{"id":6031,"date":"2019-12-30T23:28:31","date_gmt":"2019-12-31T07:28:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6031"},"modified":"2019-12-30T23:32:19","modified_gmt":"2019-12-31T07:32:19","slug":"leetcode-2-add-two-numbers-2","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2-add-two-numbers-2\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2. Add Two Numbers"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 2. Add Two Numbers - \u5237\u9898\u627e\u5de5\u4f5c EP291\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/-UBiYuIVErM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\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>&nbsp;and each of their nodes contain a single digit. Add the two numbers and return it 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><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> (2 -&gt; 4 -&gt; 3) + (5 -&gt; 6 -&gt; 4)\n<strong>Output:<\/strong> 7 -&gt; 0 -&gt; 8\n<strong>Explanation:<\/strong> 342 + 465 = 807.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(max(n,m))<br>Space complexity: O(max(n,m))<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\n    ListNode dummy(0);\n    ListNode* tail = &dummy;\n    int sum = 0;\n    while (l1 || l2 || sum) {\n      sum += (l1 ? l1->val : 0) + (l2 ? l2->val : 0);\n      l1 = l1 ? l1->next : nullptr;\n      l2 = l2 ? l2->next : nullptr;\n      tail->next = new ListNode(sum % 10);\n      sum \/= 10;\n      tail = tail->next;\n    }            \n    return dummy.next;\n  }\n};\n\n\n<\/pre>\n<\/div><\/div>\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\/\/ Author: Huahua\nclass Solution {\n  public ListNode addTwoNumbers(ListNode l1, ListNode l2) {\n    ListNode tail = new ListNode(0);\n    ListNode dummy = tail;\n    int sum = 0;\n    while (l1 != null || l2 != null || sum > 0) {\n      sum += (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val);\n      tail.next = new ListNode(sum % 10);\n      tail = tail.next;\n      if (l1 != null) l1 = l1.next;\n      if (l2 != null) l2 = l2.next;\n      sum \/= 10;\n    }\n    return dummy.next;\n  }\n}\n\n\n<\/pre>\n<\/div><\/div>\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:\n    dummy = tail = ListNode(0)\n    s = 0\n    while l1 or l2 or s:\n      s += (l1.val if l1 else 0) + (l2.val if l2 else 0)\n      tail.next = ListNode(s % 10)\n      tail = tail.next\n      s \/\/= 10\n      l1 = l1.next if l1 else None\n      l2 = l2.next if l2 else None\n    return dummy.next\n<\/pre>\n<\/div><\/div>\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&nbsp;and each of their nodes contain a single digit. Add the&#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,48],"tags":[83,31,177,179],"class_list":["post-6031","post","type-post","status-publish","format-standard","hentry","category-list","category-simulation","tag-list","tag-math","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6031","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=6031"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6031\/revisions"}],"predecessor-version":[{"id":6037,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6031\/revisions\/6037"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}