{"id":455,"date":"2020-09-26T10:43:40","date_gmt":"2020-09-26T02:43:40","guid":{"rendered":"http:\/\/39.96.58.60\/?p=455"},"modified":"2022-10-18T16:39:14","modified_gmt":"2022-10-18T08:39:14","slug":"leetcode-147-%e5%af%b9%e9%93%be%e8%a1%a8%e8%bf%9b%e8%a1%8c%e6%8f%92%e5%85%a5%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=455","title":{"rendered":"Leetcode 147. \u5bf9\u94fe\u8868\u8fdb\u884c\u63d2\u5165\u6392\u5e8f"},"content":{"rendered":"<p>\u5bf9\u94fe\u8868\u8fdb\u884c\u63d2\u5165\u6392\u5e8f\u3002<\/p>\n<p><img alt=\"\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/0\/0f\/Insertion-sort-example-300px.gif\"><\/p>\n<p><small>\u63d2\u5165\u6392\u5e8f\u7684\u52a8\u753b\u6f14\u793a\u5982\u4e0a\u3002\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u8be5\u94fe\u8868\u53ef\u4ee5\u88ab\u8ba4\u4e3a\u5df2\u7ecf\u90e8\u5206\u6392\u5e8f\uff08\u7528\u9ed1\u8272\u8868\u793a\uff09\u3002<\/small><\/p>\n<p><small><br \/>\n<\/small><small>\u6bcf\u6b21\u8fed\u4ee3\u65f6\uff0c\u4ece\u8f93\u5165\u6570\u636e\u4e2d\u79fb\u9664\u4e00\u4e2a\u5143\u7d20\uff08\u7528\u7ea2\u8272\u8868\u793a\uff09\uff0c\u5e76\u539f\u5730\u5c06\u5176\u63d2\u5165\u5230\u5df2\u6392\u597d\u5e8f\u7684\u94fe\u8868\u4e2d\u3002<\/small><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\uff1a<\/strong><\/p>\n<ol>\n<li>\u63d2\u5165\u6392\u5e8f\u662f\u8fed\u4ee3\u7684\uff0c\u6bcf\u6b21\u53ea\u79fb\u52a8\u4e00\u4e2a\u5143\u7d20\uff0c\u76f4\u5230\u6240\u6709\u5143\u7d20\u53ef\u4ee5\u5f62\u6210\u4e00\u4e2a\u6709\u5e8f\u7684\u8f93\u51fa\u5217\u8868\u3002<\/li>\n<li>\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u63d2\u5165\u6392\u5e8f\u53ea\u4ece\u8f93\u5165\u6570\u636e\u4e2d\u79fb\u9664\u4e00\u4e2a\u5f85\u6392\u5e8f\u7684\u5143\u7d20\uff0c\u627e\u5230\u5b83\u5728\u5e8f\u5217\u4e2d\u9002\u5f53\u7684\u4f4d\u7f6e\uff0c\u5e76\u5c06\u5176\u63d2\u5165\u3002<\/li>\n<li>\u91cd\u590d\u76f4\u5230\u6240\u6709\u8f93\u5165\u6570\u636e\u63d2\u5165\u5b8c\u4e3a\u6b62\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> 4-&gt;2-&gt;1-&gt;3\n\n<strong>\u8f93\u51fa:<\/strong> 1-&gt;2-&gt;3-&gt;4\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> -1-&gt;5-&gt;3-&gt;4-&gt;0\n\n<strong>\u8f93\u51fa:<\/strong> -1-&gt;0-&gt;3-&gt;4-&gt;5\n\n<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6392\u5e8f\u3001 \u94fe\u8868\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a160 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8694.60% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a15.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e867.93% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def insertionSortList(self, head: ListNode) -&gt; ListNode:\n        result = ListNode(float('-inf'), next=head)\n        r = result  # \u6307\u9488r.next\u6307\u5411\u5f53\u524d\u8282\u70b9\n        while r.next:   # \u5f53\u524d\u8282\u70b9\u4e0d\u4e3aNone\u65f6\uff0c\n            if r.next.val &gt; r.val:  # \u5982\u679c\u5f53\u524d\u8282\u70b9\u540e\u4e00\u4e2a\u8282\u70b9\u5927\u4e8e\u5f53\u524d\u8282\u70b9\u503c\uff0c\u540e\u79fb\u6307\u9488\uff0c\u5904\u7406\u4e0b\u4e00\u4e2a\n                r = r.next\n            else:   # \u5c0f\u4e8e\u65f6\uff0c\u6267\u884c\u63d2\u5165\u64cd\u4f5c\n                node = r.next   # \u8bb0\u5f55\u5e76\u5220\u9664\u5f53\u524d\u8282\u70b9node\n                p = result\n                r.next = r.next.next\n                while p.next.val &lt; node.val:    # \u5728\u6392\u5e8f\u597d\u7684\u94fe\u8868\u4e2d\uff0c\u5bfb\u627e\u63d2\u5165node\u7684\u4f4d\u7f6e\n                    p = p.next\n                node.next = p.next  # \u63d2\u5165\u8282\u70b9\n                p.next = node\n        return result.next<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u94fe\u8868\u8fdb\u884c\u63d2\u5165\u6392\u5e8f\u3002 \u63d2\u5165\u6392\u5e8f\u7684\u52a8\u753b\u6f14\u793a\u5982\u4e0a\u3002\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u8be5\u94fe\u8868\u53ef\u4ee5\u88ab\u8ba4\u4e3a\u5df2\u7ecf\u90e8\u5206\u6392\u5e8f\uff08\u7528\u9ed1\u8272\u8868\u793a\uff09\u3002 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/455"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=455"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/455\/revisions"}],"predecessor-version":[{"id":456,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/455\/revisions\/456"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}