{"id":8926,"date":"2021-11-28T23:08:40","date_gmt":"2021-11-29T07:08:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8926"},"modified":"2021-11-28T23:09:10","modified_gmt":"2021-11-29T07:09:10","slug":"leetcode-143-reorder-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-143-reorder-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 143. Reorder List"},"content":{"rendered":"\n<p>You are given the head of a singly linked-list. The list can be represented as:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">L<sub>0<\/sub> \u2192 L<sub>1<\/sub> \u2192 \u2026 \u2192 L<sub>n - 1<\/sub> \u2192 L<sub>n<\/sub>\n<\/pre>\n\n\n\n<p><em>Reorder the list to be on the following form:<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">L<sub>0<\/sub> \u2192 L<sub>n<\/sub> \u2192 L<sub>1<\/sub> \u2192 L<sub>n - 1<\/sub> \u2192 L<sub>2<\/sub> \u2192 L<sub>n - 2<\/sub> \u2192 \u2026\n<\/pre>\n\n\n\n<p>You may not modify the values in the list&#8217;s nodes. Only nodes themselves may be changed.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/04\/reorder1linked-list.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,2,3,4]\n<strong>Output:<\/strong> [1,4,2,3]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/09\/reorder2-linked-list.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,2,3,4,5]\n<strong>Output:<\/strong> [1,5,2,4,3]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the list is in the range&nbsp;<code>[1, 5 * 10<sup>4<\/sup>]<\/code>.<\/li><li><code>1 &lt;= Node.val &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Three steps<\/strong><\/h2>\n\n\n\n<p>Step 1: Find mid node that splits the list into two halves.<br>Step 2: Reverse the second half<br>Step 3: Merge two lists<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  void reorderList(ListNode* head) {\n    if (!head || !head->next) return;\n    ListNode* slow = head;\n    ListNode* fast = head;            \n\n    while (fast->next && fast->next->next) {\n      slow = slow->next;\n      fast = fast->next->next;          \n    }\n    \n    ListNode* h1 = head;\n    ListNode* h2 = reverse(slow->next);\n    slow->next = nullptr;\n\n    while (h1 && h2) {\n      ListNode* n1 = h1->next;\n      ListNode* n2 = h2->next;\n      h1->next = h2;\n      h2->next = n1;\n      h1 = n1;\n      h2 = n2;\n    }\n  }\nprivate:       \n  \/\/ reverse a linked list\n  \/\/ returns head of the linked list\n  ListNode* reverse(ListNode* head) {\n    if (!head || !head->next) return head;\n\n    ListNode* prev = head;\n    ListNode* cur = head->next;\n    ListNode* next = cur;\n    while (cur) {\n      next = next->next;\n      cur->next = prev;\n      prev = cur;\n      cur = next;\n    }\n\n    head->next = nullptr;\n    return prev;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given the head of a singly linked-list. The list can be represented as: L0 \u2192 L1 \u2192 \u2026 \u2192 Ln &#8211; 1 \u2192&#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":[352,83,177,246],"class_list":["post-8926","post","type-post","status-publish","format-standard","hentry","category-list","tag-fast-slow","tag-list","tag-medium","tag-reverse","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8926","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=8926"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8926\/revisions"}],"predecessor-version":[{"id":8928,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8926\/revisions\/8928"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}