{"id":10272,"date":"2025-04-05T13:20:38","date_gmt":"2025-04-05T20:20:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10272"},"modified":"2025-04-05T13:23:42","modified_gmt":"2025-04-05T20:23:42","slug":"leetcode-430-flatten-a-multilevel-doubly-linked-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-430-flatten-a-multilevel-doubly-linked-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 430. Flatten a Multilevel Doubly Linked List"},"content":{"rendered":"\n<p>\u7528\u9012\u5f52\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\u4e00\u70b9\u3002\u5982\u679c\u9700\u8981\u5b8c\u7f8e\u7684one pass\u5219\u9700\u8981\u5199\u4e00\u4e2ahelper function\uff0c\u8fd4\u56deflattern\u540e\u7684head\u548ctail.<\/p>\n\n\n\n<p>\u6ce8\u610f\u8fd9\u662f\u53cc\u5411\u94fe\u8868\uff0c\u8981\u628aA\u628aB\u8fde\u63a5\u5230\u4e00\u8d77\uff0c\u9700\u8981\u540c\u65f6\u6539\u4e24\u4e2a\u6307\u9488A->next = B, B->prev = A\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n)<br>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n) \/ stack<\/p>\n\n\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  Node* flatten(Node* head) {\n    return solve(head).first;\n  }\n private:\n  \/\/ Flaten head, return new head and new tail.\n  pair<Node*, Node*> solve(Node* head) {\n    if (!head) return {nullptr, nullptr};\n    Node dummy;\n    Node* cur = &dummy;\n    cur->next = head;\n    while (cur->next) {\n      cur = cur->next;\n      if (cur->child) {\n        auto [h, t] = solve(cur->child);\n        t->next = cur->next;\n        h->prev = cur;\n        if (cur->next) {\n          cur->next->prev = t;\n        }\n        cur->next = h;\n        cur->child = nullptr;\n      }\n    }\n    return {dummy.next, cur};\n  }\n};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7528\u9012\u5f52\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\u4e00\u70b9\u3002\u5982\u679c\u9700\u8981\u5b8c\u7f8e\u7684one pass\u5219\u9700\u8981\u5199\u4e00\u4e2ahelper function\uff0c\u8fd4\u56deflattern\u540e\u7684head\u548ctail. \u6ce8\u610f\u8fd9\u662f\u53cc\u5411\u94fe\u8868\uff0c\u8981\u628aA\u628aB\u8fde\u63a5\u5230\u4e00\u8d77\uff0c\u9700\u8981\u540c\u65f6\u6539\u4e24\u4e2a\u6307\u9488A->next = B, B->prev = A\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n)\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n) \/ stack class Solution { public: Node* flatten(Node* head) { return solve(head).first; } private: \/\/&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[834,83,835],"class_list":["post-10272","post","type-post","status-publish","format-standard","hentry","category-list","tag-doubly-linked-list","tag-list","tag-recurrsion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10272"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10272\/revisions"}],"predecessor-version":[{"id":10274,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10272\/revisions\/10274"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}