Reorder List

Medium

Topics
Linked ListTwo PointersStack

Given the head of a singly linked list L0 → L1 → ... → Ln-1 → Ln, reorder it to L0 → Ln → L1 → Ln-1 → L2 → .... Return the reordered list. (LeetCode mutates in place and returns void; here return the head.)

Example 1

before1234null
after1423null
pulled from the back
The result alternates front, back, front, back.
Linked list values from head to tail.
Input:  head = [1,2,3,4]
Output: [1,4,2,3]

Example 2

before12345null
after15243null
pulled from the back
Input:  head = [1,2,3,4,5]
Output: [1,5,2,4,3]

Constraints

  • 1 <= number of nodes <= 5*10^4
  • 0 <= Node.val <= 1000
Editorial — intuition, approach, complexity and pitfalls

Intuition

The target order interleaves the list with its own reverse. Classic approach: find the middle, reverse the second half, then weave the two halves together.

Approach

  1. Find the midpoint with slow/fast pointers.
  2. Reverse the second half.
  3. Merge the first and reversed-second halves alternately.

Dry Run

list 1 -> 2 -> 3 -> 4 -> 5.  Four phases, each a standard move.
  1  find the midpoint with slow and fast     slow stops on 3
  2  CUT:  first half  1 -> 2 -> 3
           second half 4 -> 5
  3  reverse the second half                  5 -> 4
  4  weave alternately                        1 then 5, then 2 then 4
answer 1 -> 5 -> 2 -> 4 -> 3
The cut in phase 2 is the line that cannot be skipped. Without `slow.next = None`
the first half still points into the second, and once the second half is reversed
those two pointers close a loop -- on [1,2,3,4] the weave then never terminates,
which measures as a hang rather than a wrong answer.
`fast` starts at `head.next` rather than `head`, which decides where slow lands on
an odd-length list. Here slow stops on 3 so the first half gets the extra node,
which is what leaves the 3 at the END of the output.
The weave saves both successors before re-pointing anything -- `f2` and `s2` --
for the same reason reverse-linked-list saves `nxt`: the next statement destroys
the pointer it would otherwise need.

Complexity

  • Time — O(n): find-middle, reverse, and merge are each linear.
  • Space — O(1): pointer surgery, no extra list.

Common Pitfalls

  • Cut the first half (slow.next = None) before reversing, or you create a cycle.

Full Solution — all languages

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def reorderList(self, head):
        if not head or not head.next: return head
        slow,fast=head,head.next
        while fast and fast.next:
            slow=slow.next; fast=fast.next.next
        second=slow.next; slow.next=None
        prev=None
        while second:
            nxt=second.next; second.next=prev; prev=second; second=nxt
        first,second=head,prev
        while second:
            f2,s2=first.next,second.next
            first.next=second; second.next=f2
            first,second=f2,s2
        return head
SensAI
Beta
Tap the mic to stop voice mode