↓Skip to main content
  1. LeetCode/

LeetCode 143: Reorder List

·2 mins· ·
LeetCode Medium Linked-List Two-Pointers Stack Recursion
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-29

  • Submissions: Runtime Error ×3 → Wrong Answer ×2 → Runtime Error → Wrong Answer ×2 → Runtime Error → Accepted → Time Limit Exceeded → Accepted ×3

Approach
#

Head, tail, second, second-to-last, … is the first half merged alternately with the reversed second half (Linked List Templates 2 and 3):

  1. Find the middle with fast and slow pointers and cut after it.
  2. Reverse the second half.
  3. Merge alternately; the first half is at least as long as the second.

My Attempts
#

This took 14 submissions in 2022. It combines three operations, which I split into three functions, but small errors kept appearing:

  • Finding the middle with while fast and fast.next.next raised when fast.next was None for a single node, e.g. [8].
  • After reversing it returned prev.next instead of prev, dropping a node.
  • One version never cut the first half, so merging linked back into the second half and formed a cycle; [9, 1] never finished.

Solution
#

class Solution:
    def reorderList(self, head: Optional[ListNode]) -> None:
        slow = fast = head
        while fast and fast.next:
            slow, fast = slow.next, fast.next.next
        second, slow.next = slow.next, None          # 切斷前半段
        prev = None
        while second:                                # 反轉後半段
            second.next, prev, second = prev, second, second.next
        first, second = head, prev
        while second:                                # 交錯合併
            n1, n2 = first.next, second.next
            first.next, second.next = second, n1
            first, second = n1, n2

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Always cut the first half, or merging links back into the second half and forms a cycle.
  • Modify in place; there is no return value.

Interview Explanation
#

I split at the middle with fast and slow pointers, reverse the second half, and interleave the two in place. Time O(n), space O(1).

Related

LeetCode 234: Palindrome Linked List
·2 mins
LeetCode Easy Linked-List Two-Pointers Stack Recursion
LeetCode 234 note: find the middle with fast and slow pointers, reverse the second half, and compare from both ends with O(1) extra space
LeetCode 82: Remove Duplicates from Sorted List II
·2 mins
LeetCode Medium Linked-List Two-Pointers
LeetCode 82 note: every duplicated value goes, the head included, so use a dummy; on a duplicate skip to the next different value and link prev.next past it
LeetCode 86: Partition List
·2 mins
LeetCode Medium Linked-List Two-Pointers
LeetCode 86 note: two dummies collect nodes below x and at least x, then join them, setting the second part’s tail to None
LeetCode 24: Swap Nodes in Pairs
·2 mins
LeetCode Medium Linked-List Recursion
LeetCode 24 note: a dummy plus pairs, rewiring prev.next, a.next and b.next together; swap nodes, not values
LeetCode 19: Remove Nth Node From End of List
·2 mins
LeetCode Medium Linked-List Two-Pointers
LeetCode 19 note: from a dummy, move fast n + 1 steps ahead, then move both to the end; slow stops before the node to delete
LeetCode 2: Add Two Numbers
·2 mins
LeetCode Medium Linked-List Math Recursion
LeetCode 2 note: digits are stored in reverse, so add from the head digit by digit with a carry, looping until both lists end and no carry remains