{"id":206,"date":"2024-11-28T09:17:16","date_gmt":"2024-11-28T01:17:16","guid":{"rendered":"http:\/\/getzs.online\/?p=206"},"modified":"2024-11-30T10:34:21","modified_gmt":"2024-11-30T02:34:21","slug":"leetcode-hot-100-3","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2024\/11\/28\/leetcode-hot-100-3\/","title":{"rendered":"LeetCode Hot100 -3"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/merge-two-sorted-lists\/description\/?envType=study-plan-v2&amp;envId=top-100-liked\">21. \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868 &#8211; \u529b\u6263\uff08LeetCode\uff09<\/a><\/p>\n\n\n\n<p>\u5c06\u4e24\u4e2a\u5347\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684&nbsp;<strong>\u5347\u5e8f<\/strong>&nbsp;\u94fe\u8868\u5e76\u8fd4\u56de\u3002\u65b0\u94fe\u8868\u662f\u901a\u8fc7\u62fc\u63a5\u7ed9\u5b9a\u7684\u4e24\u4e2a\u94fe\u8868\u7684\u6240\u6709\u8282\u70b9\u7ec4\u6210\u7684\u3002&nbsp;<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"302\" src=\"http:\/\/hello.getzs.online\/wp-content\/uploads\/2024\/11\/merge_ex1.jpg\" alt=\"\" class=\"wp-image-207\" srcset=\"https:\/\/hello.getzs.online\/wp-content\/uploads\/2024\/11\/merge_ex1.jpg 662w, https:\/\/hello.getzs.online\/wp-content\/uploads\/2024\/11\/merge_ex1-300x137.jpg 300w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>l1 = [1,2,4], l2 = [1,3,4]<br><strong>\u8f93\u51fa\uff1a<\/strong>[1,1,2,3,4,4]<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>l1 = [], l2 = []<br><strong>\u8f93\u51fa\uff1a<\/strong>[]<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>l1 = [], l2 = [0]<br><strong>\u8f93\u51fa\uff1a<\/strong>[0]<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e24\u4e2a\u94fe\u8868\u7684\u8282\u70b9\u6570\u76ee\u8303\u56f4\u662f&nbsp;<code>[0, 50]<\/code><\/li>\n\n\n\n<li><code>-100 &lt;= Node.val &lt;= 100<\/code><\/li>\n\n\n\n<li><code>l1<\/code>&nbsp;\u548c&nbsp;<code>l2<\/code>&nbsp;\u5747\u6309&nbsp;<strong>\u975e\u9012\u51cf\u987a\u5e8f<\/strong>&nbsp;\u6392\u5217<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u62df\u505a\u6cd5\uff1a<\/h2>\n\n\n\n<p>\u901a\u8fc7\u5224\u65ad\u6bcf\u4e2a\u8282\u70b9\u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u6a21\u62df\u524d\u540e\u4f4d\u7f6e\uff0c\u5f53\u67d0\u4e00\u4e2a\u4e3a\u7a7a\u7684\u65f6\u5019\uff0c\u53ea\u8981\u63a5\u4e0a\u53e6\u5916\u4e00\u4e2a\u5373\u53ef\u3002\u590d\u6742\u5ea6O(n+m)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {\n        ListNode head = new ListNode();\n        ListNode root = head;\n        while (list1 != null &amp;&amp; list2 != null) {\n            if (list1.val &lt;= list2.val) {\n                head.next = list1;\n                list1 = list1.next;\n            }\n            else {\n                head.next = list2;\n                list2 = list2.next;\n            }\n            head = head.next;\n        }\n        while (list1 != null) {\n            head.next = list1;\n            list1 = list1.next;\n            head = head.next;\n        }\n        while (list2 != null) {\n            head.next = list2;\n            list2 = list2.next;\n            head = head.next;\n        }\n        return root.next;\n    }<\/code><\/pre>\n\n\n\n<p>\u4f18\u5316\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {\n        ListNode head = new ListNode();\n        ListNode root = head;\n        while (list1 != null &amp;&amp; list2 != null) {\n            if (list1.val &lt;= list2.val) {\n                head.next = list1;\n                list1 = list1.next;\n            }\n            else {\n                head.next = list2;\n                list2 = list2.next;\n            }\n            head = head.next;\n        }\n        if (list1 != null) {\n            head.next = list1;\n        }\n        if (list2 != null) {\n            head.next = list2;\n        }\n        return root.next;\n    }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u9012\u5f52\u505a\u6cd5\uff1a<\/h2>\n\n\n\n<p>\u4f18\u5316\u6a21\u62df\u6b65\u9aa4\uff0c\u539f\u7406\u76f8\u540c\u3002\u590d\u6742\u5ea6O(n+m)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {\n        if(list1 == null)\n            return list2;\n        if(list2 == null)\n            return list1;\n        if(list1.val &gt;= list2.val){\n            list2.next = mergeTwoLists(list1, list2.next);\n            return list2;\n        }\n        else{\n            list1.next = mergeTwoLists(list1.next, list2);\n            return list1;\n        }\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>21. \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868 &#8211; \u529b\u6263\uff08LeetCode\uff09 \u5c06\u4e24\u4e2a\u5347\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684&nbsp;\u5347 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[3],"class_list":["post-206","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-leetcode-hot100"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":3,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":237,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/206\/revisions\/237"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}