{"id":2033,"date":"2022-01-14T15:29:38","date_gmt":"2022-01-14T07:29:38","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2033"},"modified":"2022-01-14T15:29:40","modified_gmt":"2022-01-14T07:29:40","slug":"leetcodeday21","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/14\/leetcodeday21\/","title":{"rendered":"leetcodeday21 &#8211;\u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868"},"content":{"rendered":"\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<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/merge_ex1.jpg\" alt=\"\" width=\"411\" height=\"187\"\/><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>l1 = &#91;1,2,4], l2 = &#91;1,3,4]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;1,1,2,3,4,4]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>l1 = &#91;], l2 = &#91;]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>l1 = &#91;], l2 = &#91;0]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;0]<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u4e24\u4e2a\u94fe\u8868\u7684\u8282\u70b9\u6570\u76ee\u8303\u56f4\u662f&nbsp;<code>[0, 50]<\/code><\/li><li><code>-100 &lt;= Node.val &lt;= 100<\/code><\/li><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><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\r\n# Definition for singly-linked list.\r\n# class ListNode:\r\n#     def __init__(self, val=0, next=None):\r\n#         self.val = val\r\n#         self.next = next\r\nclass Solution:\r\n    def mergeTwoLists(self, list1: Optional&#91;ListNode], list2: Optional&#91;ListNode]) -> Optional&#91;ListNode]:\r\r\n        list4=list3=ListNode()\r\n        while list1!=None  or list2!=None :\r\n          if list1!=None and list2!=None :  \r\n           if list1.val&lt;=list2.val:\r\n                list3.next= list1\r\n                list3=list3.next \r\n                list1=list1.next\r\n           else :\r\n                list3.next=list2\r\n                list2=list2.next\r\n                list3=list3.next \r\n          elif list1:\r\n           list3.next= list1\r\n           list3=list3.next \r\n           break\r\n          else:\r\n           list3.next= list2\r\n           list3=list3.next \r\n           break\r\n        print(list4.next)\r\n        return list4.next<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-217.png\" alt=\"\" class=\"wp-image-2034\" width=\"496\" height=\"153\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-217.png 650w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-217-300x92.png 300w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ee3\u7801\u4f18\u5316\uff1a<\/p>\n\n\n\n<p><strong>\u601d\u8def<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u7528\u8fed\u4ee3\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e0a\u8ff0\u7b97\u6cd5\u3002\u5f53&nbsp;<code>l1<\/code>&nbsp;\u548c&nbsp;<code>l2<\/code>&nbsp;\u90fd\u4e0d\u662f\u7a7a\u94fe\u8868\u65f6\uff0c\u5224\u65ad&nbsp;<code>l1<\/code>&nbsp;\u548c&nbsp;<code>l2<\/code>&nbsp;\u54ea\u4e00\u4e2a\u94fe\u8868\u7684\u5934\u8282\u70b9\u7684\u503c\u66f4\u5c0f\uff0c\u5c06\u8f83\u5c0f\u503c\u7684\u8282\u70b9\u6dfb\u52a0\u5230\u7ed3\u679c\u91cc\uff0c\u5f53\u4e00\u4e2a\u8282\u70b9\u88ab\u6dfb\u52a0\u5230\u7ed3\u679c\u91cc\u4e4b\u540e\uff0c\u5c06\u5bf9\u5e94\u94fe\u8868\u4e2d\u7684\u8282\u70b9\u5411\u540e\u79fb\u4e00\u4f4d\u3002<\/p>\n\n\n\n<p><strong>\u7b97\u6cd5<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u8bbe\u5b9a\u4e00\u4e2a\u54e8\u5175\u8282\u70b9&nbsp;<code>prehead<\/code>&nbsp;\uff0c\u8fd9\u53ef\u4ee5\u5728\u6700\u540e\u8ba9\u6211\u4eec\u6bd4\u8f83\u5bb9\u6613\u5730\u8fd4\u56de\u5408\u5e76\u540e\u7684\u94fe\u8868\u3002\u6211\u4eec\u7ef4\u62a4\u4e00\u4e2a&nbsp;<code>prev<\/code>&nbsp;\u6307\u9488\uff0c\u6211\u4eec\u9700\u8981\u505a\u7684\u662f\u8c03\u6574\u5b83\u7684&nbsp;<code>next<\/code>&nbsp;\u6307\u9488\u3002\u7136\u540e\uff0c\u6211\u4eec\u91cd\u590d\u4ee5\u4e0b\u8fc7\u7a0b\uff0c\u76f4\u5230&nbsp;<code>l1<\/code>&nbsp;\u6216\u8005&nbsp;<code>l2<\/code>&nbsp;\u6307\u5411\u4e86&nbsp;<code>null<\/code>&nbsp;\uff1a\u5982\u679c&nbsp;<code>l1<\/code>&nbsp;\u5f53\u524d\u8282\u70b9\u7684\u503c\u5c0f\u4e8e\u7b49\u4e8e&nbsp;<code>l2<\/code>&nbsp;\uff0c\u6211\u4eec\u5c31\u628a&nbsp;<code>l1<\/code>&nbsp;\u5f53\u524d\u7684\u8282\u70b9\u63a5\u5728&nbsp;<code>prev<\/code>&nbsp;\u8282\u70b9\u7684\u540e\u9762\u540c\u65f6\u5c06&nbsp;<code>l1<\/code>&nbsp;\u6307\u9488\u5f80\u540e\u79fb\u4e00\u4f4d\u3002\u5426\u5219\uff0c\u6211\u4eec\u5bf9&nbsp;<code>l2<\/code>&nbsp;\u505a\u540c\u6837\u7684\u64cd\u4f5c\u3002\u4e0d\u7ba1\u6211\u4eec\u5c06\u54ea\u4e00\u4e2a\u5143\u7d20\u63a5\u5728\u4e86\u540e\u9762\uff0c\u6211\u4eec\u90fd\u9700\u8981\u628a&nbsp;<code>prev<\/code>&nbsp;\u5411\u540e\u79fb\u4e00\u4f4d\u3002<\/p>\n\n\n\n<p>\u5728\u5faa\u73af\u7ec8\u6b62\u7684\u65f6\u5019\uff0c&nbsp;<code>l1<\/code>&nbsp;\u548c&nbsp;<code>l2<\/code>&nbsp;\u81f3\u591a\u6709\u4e00\u4e2a\u662f\u975e\u7a7a\u7684\u3002\u7531\u4e8e\u8f93\u5165\u7684\u4e24\u4e2a\u94fe\u8868\u90fd\u662f\u6709\u5e8f\u7684\uff0c\u6240\u4ee5\u4e0d\u7ba1\u54ea\u4e2a\u94fe\u8868\u662f\u975e\u7a7a\u7684\uff0c\u5b83\u5305\u542b\u7684\u6240\u6709\u5143\u7d20\u90fd\u6bd4\u524d\u9762\u5df2\u7ecf\u5408\u5e76\u94fe\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u8981\u5927\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ea\u9700\u8981\u7b80\u5355\u5730\u5c06\u975e\u7a7a\u94fe\u8868\u63a5\u5728\u5408\u5e76\u94fe\u8868\u7684\u540e\u9762\uff0c\u5e76\u8fd4\u56de\u5408\u5e76\u94fe\u8868\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\r\n# Definition for singly-linked list.\r\n# class ListNode:\r\n#     def __init__(self, val=0, next=None):\r\n#         self.val = val\r\n#         self.next = next\r\nclass Solution:\r\n    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:\r\n        prehead = ListNode(-1)\r\n\r\n        prev = prehead\r\n        while l1 and l2:\r\n            if l1.val &lt;= l2.val:\r\n                prev.next = l1\r\n                l1 = l1.next\r\n            else:\r\n                prev.next = l2\r\n                l2 = l2.next            \r\n            prev = prev.next\r\n\r\n        # \u5408\u5e76\u540e l1 \u548c l2 \u6700\u591a\u53ea\u6709\u4e00\u4e2a\u8fd8\u672a\u88ab\u5408\u5e76\u5b8c\uff0c\u6211\u4eec\u76f4\u63a5\u5c06\u94fe\u8868\u672b\u5c3e\u6307\u5411\u672a\u5408\u5e76\u5b8c\u7684\u94fe\u8868\u5373\u53ef\r\n        prev.next = l1 if l1 is not None else l2\r\n\r\n        return prehead.next<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06\u4e24\u4e2a\u5347\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684&nbsp;\u5347\u5e8f&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 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/14\/leetcodeday21\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday21 &#8211;\u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2033"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2033"}],"version-history":[{"count":2,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2033\/revisions"}],"predecessor-version":[{"id":2036,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2033\/revisions\/2036"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2033"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}