{"id":565,"date":"2020-09-26T10:58:18","date_gmt":"2020-09-26T02:58:18","guid":{"rendered":"http:\/\/39.96.58.60\/?p=565"},"modified":"2022-10-18T16:38:45","modified_gmt":"2022-10-18T08:38:45","slug":"leetcode-328-%e5%a5%87%e5%81%b6%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=565","title":{"rendered":"Leetcode 328. \u5947\u5076\u94fe\u8868"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5355\u94fe\u8868\uff0c\u628a\u6240\u6709\u7684\u5947\u6570\u8282\u70b9\u548c\u5076\u6570\u8282\u70b9\u5206\u522b\u6392\u5728\u4e00\u8d77\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u5947\u6570\u8282\u70b9\u548c\u5076\u6570\u8282\u70b9\u6307\u7684\u662f\u8282\u70b9\u7f16\u53f7\u7684\u5947\u5076\u6027\uff0c\u800c\u4e0d\u662f\u8282\u70b9\u7684\u503c\u7684\u5947\u5076\u6027\u3002<\/p>\n<p>\u8bf7\u5c1d\u8bd5\u4f7f\u7528\u539f\u5730\u7b97\u6cd5\u5b8c\u6210\u3002\u4f60\u7684\u7b97\u6cd5\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u5e94\u4e3a O(1)\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5e94\u4e3a O(nodes)\uff0cnodes \u4e3a\u8282\u70b9\u603b\u6570\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL\n\n<strong>\u8f93\u51fa:<\/strong> 1-&gt;3-&gt;5-&gt;2-&gt;4-&gt;NULL\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> 2-&gt;1-&gt;3-&gt;5-&gt;6-&gt;4-&gt;7-&gt;NULL \n\n<strong>\u8f93\u51fa:<\/strong> 2-&gt;3-&gt;6-&gt;7-&gt;1-&gt;5-&gt;4-&gt;NULL<\/pre>\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n<ul>\n<li>\u5e94\u5f53\u4fdd\u6301\u5947\u6570\u8282\u70b9\u548c\u5076\u6570\u8282\u70b9\u7684\u76f8\u5bf9\u987a\u5e8f\u3002<\/li>\n<li>\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u89c6\u4e3a\u5947\u6570\u8282\u70b9\uff0c\u7b2c\u4e8c\u4e2a\u8282\u70b9\u89c6\u4e3a\u5076\u6570\u8282\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \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\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8699.25% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a15.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.20% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u53cc\u6307\u9488\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def oddEvenList(self, head: ListNode) -&gt; ListNode:\n        if not (head and head.next and head.next.next):\n            return head\n\n        p, q = head, head.next  # \u53cc\u6307\u9488\uff0cp\u6307\u5411\u63d2\u5165\u5904\uff0cq\u6307\u5411\u5f85\u63d2\u5165\u8282\u70b9\u524d\u4e00\u4e2a\n\n        while q and q.next:\n            node = q.next       # \u53d6\u51fanode\n            q.next = node.next  # \u5220\u6389node\uff0c\u8fde\u63a5\u5220\u540e\u7684\u94fe\u8868\n\n            p_next = p.next     # \u8bb0\u5f55p\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n            p.next = node       # \u63d2\u5165node\n            node.next = p_next  # \u8fde\u63a5\u94fe\u8868\n\n            p = p.next  # \u79fb\u52a8\u6307\u9488\n            q = q.next\n        return head<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5355\u94fe\u8868\uff0c\u628a\u6240\u6709\u7684\u5947\u6570\u8282\u70b9\u548c\u5076\u6570\u8282\u70b9\u5206\u522b\u6392\u5728\u4e00\u8d77\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u5947\u6570\u8282\u70b9\u548c\u5076\u6570\u8282\u70b9\u6307\u7684\u662f\u8282\u70b9\u7f16\u53f7\u7684\u5947\u5076\u6027&#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\/565"}],"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=565"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/565\/revisions"}],"predecessor-version":[{"id":566,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/565\/revisions\/566"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=565"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}