Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    [LeetCode 143] Reorder List

    Published Jun 10, 2022 [  LinkedList  ]

    Problem

    You are given the head of a singly linked-list. The list can be represented as:

    L0 → L1 → … → Ln - 1 → Ln
    

    Reorder the list to be on the following form:

    L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
    

    You may not modify the values in the list’s nodes. Only nodes themselves may be changed.

    Example 1:

    Input: head = [1,2,3,4]
    Output: [1,4,2,3]
    

    Example 2:

    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 [1, 5 * 10^4].
    • 1 <= Node.val <= 1000

    Thoughts

    • Use slow & fast pointer to find mid pointer
    • Reverse the second part
    • Then do merge

    TypeScript

    /**
     * Definition for singly-linked list.
     * class ListNode {
     *     val: number
     *     next: ListNode | null
     *     constructor(val?: number, next?: ListNode | null) {
     *         this.val = (val===undefined ? 0 : val)
     *         this.next = (next===undefined ? null : next)
     *     }
     * }
     */
    
    /**
     Do not return anything, modify head in-place instead.
     */
    function reorderList(head: ListNode | null): void {
        let slow: ListNode = head, fast: ListNode = head.next;
        
        // move slow to the mid
        while(fast && fast.next){
            slow = slow.next
            fast = fast.next.next
        }
    
        // reverse the second part
        let second: ListNode = slow.next;
        // end first
        slow.next = null
        let prev: ListNode = null
        while(second){
            const temp: ListNode = second.next
            second.next = prev
            prev = second;
            second = temp;
        }
    
        // merge now
        let first: ListNode = head;
        second = prev;
    
        while(second){
            const temp1: ListNode = first.next;
            const temp2: ListNode = second.next;
            first.next = second
            second.next = temp1;
            first = temp1
            second = temp2
        }
    };
    

    Reference