all entries
A-037Day 372026.06.12mediumleetcode #143neetcode150

Reorder List

#linked-list#two-pointers#stack#recursion
leetcode #143 · reorder-list
01

Problem

· problem
P.037

Reorder List

leetcode #143

You are given the head of a singly linked-list. The list can be represented as: L₀ → L₁ → … → Lₙ₋₁ → Lₙ Reorder the list to be on the following form: L₀ → Lₙ → L₁ → Lₙ₋₁ → L₂ → Lₙ₋₂ → … You may not modify the values in the list's nodes. Only nodes themselves may be changed.

constraints
  • · 1 ≤ number of nodes ≤ 5 × 10⁴
  • · 1 ≤ Node.val ≤ 1000
// paraphrased summary — see source for full text
examples
example 1input → output
[1,2,3,4]
[1,4,2,3]
example 2input → output
[1,2,3,4,5]
[1,5,2,4,3]
02

Pre-solve

· pre-solve
● 1list shown● 2select● 3reveal
  • Can we modify node pointers?
  • What if the list has only one node?
  • When the list has odd length, where does the middle node end up?
  • Do we need to use additional data structures like stacks or queues?
  • How can we find the middle of the list?
  • Should we create new ListNode objects?
  • Is using a hash map to store node values efficient?
check the items you would ask, then press confirm
// session-only state — refresh resets (repeatable practice)
03

Logic Structure

· logic
● 1slots shown● 2pick per slot● 3reveal
// pick one code line per slot to assemble the algorithm flow. no typing — just the logic skeleton.
step 1· Initialize pointers
slow, fast = head, head.next
slow, fast = head, head
slow, fast = head.next, head
step 2· Find middle of listnested
while fast and fast.next:
while fast:
while fast and fast.next and fast.next.next:
step 3· Separate list halves
second = slow.next
second = slow
second = slow.next.next
step 4· Cut list at middle
prev = slow.next = None
prev = None
step 5· Reverse second halfnested
while second:
while second.next:
step 6· Merge two halvesnested
while second:
while first:
first.next = second; first = second
pick one option per slot
// format: slot — recursive / DP patterns use ordering / state-first formats. ADR-08 follow-up.
04

Solve · Trace

· solve
solution.py
1
class Solution:
2
    def reorderList(self, head: ListNode) -> None:
3
        # find middle
4
        slow, fast = head, head.next
5
        while fast and fast.next:
6
            slow = slow.next
7
            fast = fast.next.next
8
9
        # reverse second half
10
        second = slow.next
11
        prev = slow.next = None
12
        while second:
13
            tmp = second.next
14
            second.next = prev
15
            prev = second
16
            second = tmp
17
18
        # merge two halfs
19
        first, second = head, prev
20
        while second:
21
            tmp1, tmp2 = first.next, second.next
22
            first.next = second
23
            second.next = tmp1
24
            first, second = tmp1, tmp2
mental dry-run cases
// walk each case in your head; expand the worked example below if stuck.
case 1
[1,2,3,4]
[1,4,2,3]
case 2
[1,2,3,4,5]
[1,5,2,4,3]
// UI does not walk-through — you do the dry-run mentally. Expand the worked example if stuck.