# LC 143. Reorder List ### [Problem link](https://leetcode.com/problems/reorder-list/) ###### tags: `leedcode` `python` `c++` `medium` `Linked List` You are given the head of a singly linked-list. The list can be represented as: ![](https://hackmd.io/_uploads/rJlxwv5sn.png) Reorder the list to be on the following form: ![](https://hackmd.io/_uploads/Bybfwwcjh.png) You may not modify the values in the list's nodes. Only nodes themselves may be changed. **Example 1:** <img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" /> ``` Input: head = [1,2,3,4] Output: [1,4,2,3] ``` **Example 2:** <img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" /> ``` Input: head = [1,2,3,4,5] Output: [1,5,2,4,3] ``` **Constraints:** - The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>. - <code>1 <= Node.val <= 1000</code> ## Solution 1 - Linked List #### Python ```python= # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reorderList(self, head: Optional[ListNode]) -> None: """ Do not return anything, modify head in-place instead. """ dummy = ListNode(next=head) fast = slow = dummy # split while fast and fast.next: fast = fast.next.next slow = slow.next rev = slow.next slow.next = None # reverse node = None while rev: tmp = rev.next rev.next = node node = rev rev = tmp # merge while node: tmp1, tmp2 = head.next, node.next head.next = node node.next = tmp1 head, node = tmp1, tmp2 ``` #### C++ (better than python) ```cpp= /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* reverse(ListNode *node) { ListNode *cur = node; ListNode *pre = nullptr; while (cur) { ListNode *next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; } void reorderList(ListNode* head) { if (head->next == nullptr) { return; } ListNode *fast = head; ListNode *pre = nullptr; ListNode *slow = head; while (fast && fast->next) { fast = fast->next->next; pre = slow; slow = slow->next; } pre->next = nullptr; ListNode *h1 = head; ListNode *h2 = reverse(slow); while (h1->next) { ListNode *next1 = h1->next; ListNode *next2 = h2->next; h1->next = h2; h2->next = next1; h1 = next1; h2 = next2; } h1->next = h2; return; } }; ``` >### Complexity >n = The number of nodes in the list >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(n) | O(1) | ## Note sol1: split -> reverse -> merge ``` ex1: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> None split: 1 -> 2 -> 3 -> None 4 -> 5 -> 6 -> None 反轉: 4 -> 5 -> 6 -> None => 6 -> 5 -> 4 -> None 合併: 1 -> 6 -> 2 -> 5 -> 3 -> 4 -> None ``` ``` ex2: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> None split: 1 -> 2 -> 3 -> 4 -> None 5 -> 6 -> 7 -> None 反轉: 5 -> 6 -> 7 -> None => 7 -> 6 -> 5 -> None 合併: 1 -> 7 -> 2 -> 6 -> 3 -> 5 -> 4 -> None ```