{"id":2805,"date":"2020-01-22T10:59:06","date_gmt":"2020-01-22T02:59:06","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2805"},"modified":"2020-01-22T10:59:06","modified_gmt":"2020-01-22T02:59:06","slug":"%e3%80%90leetcode%e3%80%910021-%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aa%e6%9c%89%e5%ba%8f%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2805\/","title":{"rendered":"\u3010LeetCode\u30110021 \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/merge-two-sorted-lists\/\">https:\/\/leetcode-cn.com\/problems\/merge-two-sorted-lists\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em># Definition for singly-linked list.\nclass ListNode(object):\n    def __init__(self, x):\n        self.val = x\n        self.next = None\nclass Solution(object):\n    def mergeTwoLists(self, l1, l2):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> l1: ListNode\n        <\/em><strong><em>:type<\/em><\/strong><em> l2: ListNode\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: ListNode\n        \"\"\"\n        <\/em>if l1 is None and l2 is None:\n            return\n        if l1 is None or l2 is None:\n            return l1 if l2 is None else l2\n        if l1.val > l2.val:\n            return self.mergeTwoLists(l2, l1)\n        res = ListNode(l1.val)\n        node = res\n        l1 = l1.next\n        while l1 is not None and l2 is not None:\n            if l1.val &lt; l2.val:\n                node.next = l1\n                l1 = l1.next\n            else:\n                node.next = l2\n                l2 = l2.next\n            node = node.next\n        while l1 is not None:\n            node.next = l1\n            l1 = l1.next\n            node = node.next\n        while l2 is not None:\n            node.next = l2\n            l2 = l2.next\n            node = node.next\n        return res\nif __name__ == '__main__':\n    l1 = ListNode(-9)\n    l2 = ListNode(3)\n    # l3 = ListNode(4)\n    l1.next = l2\n    # l2.next = l3\n    ll1 = ListNode(5)\n    ll2 = ListNode(7)\n    # ll3 = ListNode(4)\n    ll1.next = ll2\n    # ll2.next = ll3\n    print(Solution().mergeTwoLists(l1, ll1))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u601d\u8def\u6bd4\u8f83\u7b80\u5355\uff0c\u4f9d\u6b21\u904d\u5386\u4e24\u4e2a\u94fe\u8868\uff0c\u6bd4\u8f83\u5f53\u524d\u6807\u8bb0\u4f4d\u7684\u5927\u5c0f\uff0c\u8f83\u5c0f\u7684\u91cd\u65b0\u94fe\u5165\u65b0\u94fe\u8868\u4e2d\u3002\u5f53\u6709\u4e00\u4e2a\u94fe\u8868\u62c6\u6210\u7a7a\u65f6\u8bf4\u660e\u53e6\u4e00\u4e2a\u94fe\u8868\u4e2d\u4efb\u4f55\u4e00\u4f4d\u90fd\u5927\u3002\u56e0\u6b64\u76f4\u63a5\u94fe\u5165\u65b0\u94fe\u8868\u7684\u94fe\u5c3e\u5373\u53ef\u3002<\/p>\n\n\n<p><strong>\u601d\u8003<\/strong>\uff1a\u597d\u50cf\u6709\u7684\u5730\u65b9\u6709\u70b9\u9ebb\u70e6\u4e86\u50b2\uff0c\u6700\u540e\u5bf9\u5355\u4e00\u94fe\u8868\u63d2\u5165\u94fe\u5c3e\u7684\u64cd\u4f5c\u4e0d\u9700\u8981\u5faa\u73af\uff0c\u53ea\u9700\u8981node.next = l1\u5373\u53ef\u3002\u6bd5\u7adf\u5df2\u7ecf\u662f\u8fde\u597d\u4e86\u7684\u561b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/merge-two-sorted- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1512,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2805"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2805"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2805\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2805"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}