{"id":423,"date":"2020-09-26T10:39:47","date_gmt":"2020-09-26T02:39:47","guid":{"rendered":"http:\/\/39.96.58.60\/?p=423"},"modified":"2022-10-18T16:39:23","modified_gmt":"2022-10-18T08:39:23","slug":"leetcode-430-%e6%89%81%e5%b9%b3%e5%8c%96%e5%a4%9a%e7%ba%a7%e5%8f%8c%e5%90%91%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=423","title":{"rendered":"Leetcode 430. \u6241\u5e73\u5316\u591a\u7ea7\u53cc\u5411\u94fe\u8868"},"content":{"rendered":"<p>\u591a\u7ea7\u53cc\u5411\u94fe\u8868\u4e2d\uff0c\u9664\u4e86\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u548c\u524d\u4e00\u4e2a\u8282\u70b9\u6307\u9488\u4e4b\u5916\uff0c\u5b83\u8fd8\u6709\u4e00\u4e2a\u5b50\u94fe\u8868\u6307\u9488\uff0c\u53ef\u80fd\u6307\u5411\u5355\u72ec\u7684\u53cc\u5411\u94fe\u8868\u3002\u8fd9\u4e9b\u5b50\u5217\u8868\u4e5f\u53ef\u80fd\u4f1a\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u81ea\u5df1\u7684\u5b50\u9879\uff0c\u4f9d\u6b64\u7c7b\u63a8\uff0c\u751f\u6210\u591a\u7ea7\u6570\u636e\u7ed3\u6784\uff0c\u5982\u4e0b\u9762\u7684\u793a\u4f8b\u6240\u793a\u3002<\/p>\n<p>\u7ed9\u4f60\u4f4d\u4e8e\u5217\u8868\u7b2c\u4e00\u7ea7\u7684\u5934\u8282\u70b9\uff0c\u8bf7\u4f60\u6241\u5e73\u5316\u5217\u8868\uff0c\u4f7f\u6240\u6709\u7ed3\u70b9\u51fa\u73b0\u5728\u5355\u7ea7\u53cc\u94fe\u8868\u4e2d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,2,3,7,8,11,12,9,10,4,5,6]\n\n<strong>\u89e3\u91ca\uff1a\n\n<\/strong>\n\n\u8f93\u5165\u7684\u591a\u7ea7\u5217\u8868\u5982\u4e0b\u56fe\u6240\u793a\uff1a\n\n\n\n<img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/10\/12\/multilevellinkedlist.png\" style=\"height: 363px; width: 640px;\">\n\n\n\n\u6241\u5e73\u5316\u540e\u7684\u94fe\u8868\u5982\u4e0b\u56fe\uff1a\n\n\n\n<img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/10\/12\/multilevellinkedlistflattened.png\" style=\"height: 80px; width: 1100px;\">\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,null,3]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,3,2]\n\n<strong>\u89e3\u91ca\uff1a\n\n\n\n<\/strong>\u8f93\u5165\u7684\u591a\u7ea7\u5217\u8868\u5982\u4e0b\u56fe\u6240\u793a\uff1a\n\n\n\n  1---2---NULL\n\n  |\n\n  3---NULL\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = []\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[]\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u5982\u4f55\u8868\u793a\u6d4b\u8bd5\u7528\u4f8b\u4e2d\u7684\u591a\u7ea7\u94fe\u8868\uff1f<\/strong><\/p>\n<p>\u4ee5 <strong>\u793a\u4f8b 1<\/strong> \u4e3a\u4f8b\uff1a<\/p>\n<pre> 1---2---3---4---5---6--NULL\n\n         |\n\n         7---8---9---10--NULL\n\n             |\n\n             11--12--NULL<\/pre>\n<p>\u5e8f\u5217\u5316\u5176\u4e2d\u7684\u6bcf\u4e00\u7ea7\u4e4b\u540e\uff1a<\/p>\n<pre>[1,2,3,4,5,6,null]\n\n[7,8,9,10,null]\n\n[11,12,null]\n\n<\/pre>\n<p>\u4e3a\u4e86\u5c06\u6bcf\u4e00\u7ea7\u90fd\u5e8f\u5217\u5316\u5230\u4e00\u8d77\uff0c\u6211\u4eec\u9700\u8981\u6bcf\u4e00\u7ea7\u4e2d\u6dfb\u52a0\u503c\u4e3a null \u7684\u5143\u7d20\uff0c\u4ee5\u8868\u793a\u6ca1\u6709\u8282\u70b9\u8fde\u63a5\u5230\u4e0a\u4e00\u7ea7\u7684\u4e0a\u7ea7\u8282\u70b9\u3002<\/p>\n<pre>[1,2,3,4,5,6,null]\n\n[null,null,7,8,9,10,null]\n\n[null,11,12,null]\n\n<\/pre>\n<p>\u5408\u5e76\u6240\u6709\u5e8f\u5217\u5316\u7ed3\u679c\uff0c\u5e76\u53bb\u9664\u672b\u5c3e\u7684 null \u3002<\/p>\n<pre>[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u8282\u70b9\u6570\u76ee\u4e0d\u8d85\u8fc7 1000<\/li>\n<li><code>1 &lt;= Node.val &lt;= 10^5<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\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\uff1a36 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8697.99% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.05% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6307\u9488+\u9012\u5f52\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def flatten(self, head: 'Node') -&gt; 'Node':\n        result = Node(0)    # \u7528\u4e8e\u4fdd\u5b58\u7ed3\u679c\n        self.node = result  # \u6307\u9488\u6307\u5411\u5f53\u524d\u5904\u7406\u8282\u70b9\n        def find(head): # \u9012\u5f52\n            if head:\n                head_next = head.next   # \u4fdd\u5b58\u5f53\u524d\u8282\u70b9next\u548cchild\n                head_child = head.child\n\n                self.node.next = head   # \u5c06\u5f53\u524d\u8282\u70b9\u63d2\u5165\u5230\u6307\u9488\u540e\n                self.node.child = None  # \u5220\u9664child\n                head.prev = self.node\n                self.node = self.node.next  # \u79fb\u52a8\u6307\u9488\n\n                find(head_child)    # \u9012\u5f52\u5904\u7406child\n                find(head_next)     # \u9012\u5f52\u5904\u7406next\n        find(head)\n        if head:\n            result.next.prev = None # \u5c06\u7ed3\u679c\u7684prev\u6307\u5411None\n        return result.next<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u7ea7\u53cc\u5411\u94fe\u8868\u4e2d\uff0c\u9664\u4e86\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u548c\u524d\u4e00\u4e2a\u8282\u70b9\u6307\u9488\u4e4b\u5916\uff0c\u5b83\u8fd8\u6709\u4e00\u4e2a\u5b50\u94fe\u8868\u6307\u9488\uff0c\u53ef\u80fd\u6307\u5411\u5355\u72ec\u7684\u53cc\u5411\u94fe\u8868\u3002\u8fd9\u4e9b\u5b50\u5217&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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\/423"}],"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=423"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/423\/revisions"}],"predecessor-version":[{"id":424,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/423\/revisions\/424"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}