{"id":57,"date":"2017-09-03T11:00:03","date_gmt":"2017-09-03T18:00:03","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=57"},"modified":"2018-07-10T18:39:39","modified_gmt":"2018-07-11T01:39:39","slug":"leetcode-21-merge-two-sorted-lists","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-21-merge-two-sorted-lists\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 21: Merge Two Sorted Lists"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 21. Merge Two Sorted Lists - \u5237\u9898\u627e\u5de5\u4f5c EP31\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/qckKEYP9bBA?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><\/p>\n<p>Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.<\/p>\n<p><strong>Solution 1<\/strong>: Iterative\u00a0O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    Solution 1: Iterative\r\n    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {\r\n        ListNode dummy(0);\r\n        ListNode* tail=&amp;dummy;\r\n        while(l1 &amp;&amp; l2) {\r\n            if(l1-&gt;val &lt; l2-&gt;val) {\r\n                tail-&gt;next=l1;\r\n                l1=l1-&gt;next;\r\n            }else{\r\n                tail-&gt;next=l2;\r\n                l2=l2-&gt;next;\r\n            }\r\n            tail=tail-&gt;next;\r\n        }\r\n        \r\n        if(l1) tail-&gt;next = l1;\r\n        if(l2) tail-&gt;next = l2;\r\n        \r\n        return dummy.next;\r\n        \r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Solution 2<\/strong>: Recursive O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {\r\n        \/\/ If one of the list is emptry, return the other one.\r\n        if(!l1 || !l2) return l1 ? l1 : l2;\r\n        \/\/ The smaller one becomes the head.\r\n        if(l1-&gt;val &lt; l2-&gt;val) {\r\n            l1-&gt;next = mergeTwoLists(l1-&gt;next, l2);\r\n            return l1;\r\n        } else {\r\n            l2-&gt;next = mergeTwoLists(l1, l2-&gt;next);\r\n            return l2;\r\n        }\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of 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":[3,50],"tags":[83,177,129,17,185],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-leetcode","category-list","tag-list","tag-medium","tag-merge","tag-recursion","tag-sorted","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/57","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=57"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":3067,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/57\/revisions\/3067"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}