Skip to content

Reorder List

Rearrange a list as first, last, second, second-last and so on, in place with O(1) extra space.

MediumLinked Lists
Solve on LeetCode

The problem

You get the head of a singly linked list whose nodes are L0, L1, ..., Ln in order. Rearrange the nodes into the order L0, Ln, L1, Ln-1, L2, Ln-2 and so on. Change the links between the nodes, not the values inside them; the function returns nothing.

Input head = [10, 20, 30, 40, 50, 60]

Output [10, 60, 20, 50, 30, 40]

Take one node from the front, then one from the back, until the two ends meet.

Input head = [7, 1, 4, 9, 2]

Output [7, 2, 1, 9, 4]

With an odd length, the middle node (4) ends up last.

Key constraint: Only the links may change, never the values, and the intended answer uses O(1) extra space.

How to spot the pattern

  • The new order alternates between the front and the back of the list.
  • A singly linked list can't walk backwards, so the back half has to be reversed first.
  • The change has to happen in place.

The idea

Read backwards, the second half of the list is exactly the sequence of nodes you need to slot in between the nodes of the first half. So split the list in the middle, reverse the second half, and then zip the two halves together one node at a time. Each of those steps is a standard linked list move, and none of them needs extra memory.

Brute force: Copy the nodes into an array and relink them with one index from each end, which works but uses O(n) extra space.

Step by step

  1. 1Find the middle with two pointers: slow moves one node per step and fast moves two.
  2. 2Cut the list after slow, so the first half is as long as the second half or one node longer.
  3. 3Reverse the second half.
  4. 4Merge the halves: take a node from the first half, then one from the reversed second half, until the second half runs out.

See the pattern move

The linked lists pattern on a small example. Step through it, or press play.

Code

class Solution:
    def reorderList(self, head: Optional[ListNode]) -> None:
        if head is None or head.next is None:
            return

        # 1. Find the middle: slow stops at the last node of the first half.
        slow, fast = head, head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next

        # 2. Cut the list there and reverse the second half.
        second = slow.next
        slow.next = None
        prev = None
        while second:
            nxt = second.next
            second.next = prev
            prev = second
            second = nxt

        # 3. Merge: one node from the front half, then one from the reversed back half.
        first, second = head, prev
        while second:
            first_next, second_next = first.next, second.next
            first.next = second
            second.next = first_next
            first, second = first_next, second_next

Complexity

Time

O(n)

Space

O(1)

Finding the middle, reversing and merging each walk the list once, using a handful of pointers.

Watch out for

  • Not cutting the first half off (setting slow.next to null), which leaves a cycle in the result.
  • Overwriting a next pointer during the merge before saving it, which loses the rest of the list.
  • Splitting at the wrong node, so the halves differ by more than one and the merge drops nodes.

Follow-up questions

How would you check whether a list is a palindrome in O(1) space?

Use the same first steps: find the middle, reverse the second half, then compare the two halves node by node.

Could you solve it with a stack instead?

Yes: push every node, then pop nodes to get them back to front. It is simpler to write but needs O(n) extra space.

In Hinglish

Beech dhoondo, doosra half ulta karo, phir dono halves se ek-ek node utha ke jodte jao.

Related problems