LeetCode #143 Medium

Reorder List

Reorder List is LeetCode 143 (Medium). A list reads L0 → L1 → … → Ln. Rearrange it to L0 → Ln → L1 → Ln−1 → L2 → …: first node, last node, second node, second-to-last, and so on.

  • Change the links, not the values stored in the nodes.
  • Nothing is returned; the list is changed in place.
  • The list has at least one node and up to 5 · 10⁴, so an O(n²) search for each last node is too slow.
Constraints
  • The number of nodes in the list is in the range [1, 5 * 10⁴].
  • 1 <= Node.val <= 1000
linked-listtwo-pointersreversal
Open on LeetCode ↗
02

Intuition

The answer alternates between the front of the list and the back, and the back has to be read backwards. A singly linked list only goes forwards, so the trick is to make the back half go forwards by reversing it.

  • Split: find the middle, then cut the list into a front half and a back half.
  • Reverse: reverse the back half, so its first node is the original last node.
  • Weave: take one node from the front, one from the reversed back, and repeat.

Each part is a standard linked-list routine, and together they use O(1) extra memory.

How to spot this pattern

When an answer needs a singly linked list from both ends at once, reverse one half so both halves can be read forwards. Palindrome checks on a list use the same split.

03

Approach

Try it first

Before reading on, decide where to split so that the front half is never shorter than the back half, for both an odd and an even length. Then plan which next pointers you must save before relinking during the weave. Aim for O(n) time and O(1) extra space.

1

Find the end of the front half

Start slow and fast on the head. While fast has two nodes after it, move slow one step and fast two. slow stops on the last node of the front half, so the front half gets the extra node when the length is odd.

2

Cut the list in two

Let second = slow.next, then set slow.next = None. Without this cut the front half still runs into the back half, and the weave builds a cycle.

3

Reverse the back half

Walk second with the usual three-pointer reversal: save the next node, point the current node at prev, then advance. When it ends, prev is the old last node and the back half reads from the end towards the middle.

4

Weave the halves

With first on the head and second on prev, save both next nodes, link first to second and second to the saved front node, then move both on. Stop when the back half runs out; the front half's leftover node is already in place.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def reorderList(self, head: Optional[ListNode]) -> None:
▶3 slow = fast = head
▶4 while fast.next and fast.next.next:
▶5 slow = slow.next
▶6 fast = fast.next.next
▶7 second = slow.next
▶8 slow.next = None
▶9 prev = None
▶10 while second:
▶11 nxt = second.next
▶12 second.next = prev
▶13 prev = second
▶14 second = nxt
▶15 first, second = head, prev
▶16 while second:
▶17 n1, n2 = first.next, second.next
▶18 first.next = second
▶19 second.next = n1
▶20 first, second = n1, n2
list1234slow, fastbacknoneanswernoneslow and fast start on the head
slow1
fast1
fast moves two nodes for every one slow moves, so when fast runs out of room, slow has covered half the list.
list1234slowfastbacknoneanswernonefast can't jump two more → slow ends the front half
slow2
fast3
fast has only one node after it, so the loop stops. slow, on 2, is the last node of the front half; the front half is the longer one when the length is odd.
front12slowback34secondanswernone2 now ends the front half; back half starts at 3
second3
Clearing slow's link makes two separate lists. If it stayed, the front half would still run into the back half and the weave would build a loop.
front12back34prevanswernone3 now points back to nothing
prev3
second4
3 will be the last node of the reversed half, so its link becomes empty. Its old next was saved first, so the rest of the half is not lost.
front12back34prevanswernone4 now points back to 3
prev4
secondnone
4 turns around to point at 3. It was the last node, so it is now the first node of the reversed half.
front12firstback34secondanswer14take 1 from the front, then 4 from the back
first2
second3
1 links to 4, and 4 links to the saved next front node. Both next nodes were saved before relinking, so neither half is lost.
front12back34answer1423take 2 from the front, then 3 from the back
firstnone
secondnone
2 links to 3, and 3 links to the saved next front node. Both next nodes were saved before relinking, so neither half is lost.
front12back34answer1423back half used up; list: 1 → 4 → 2 → 3
secondnone
Both halves had the same length, so the last back node was woven in last and its link is already empty.
05

Common pitfalls

Forgetting to cut after the middle

✗ Wrong
second = slow.next
# slow.next still points into the back half
✓ Right
second = slow.next
slow.next = None

On [1, 2, 3, 4], node 2 still points at 3 after the back half 3 → 4 is reversed to 4 → 3. In the last weave first is 2 and second is 3, and 2's old next is also 3, so 3 gets linked to itself: a loop the judge never finishes printing.

Reading first.next after changing it

✗ Wrong
first.next = second
second.next = first.next  # already second
✓ Right
n1 = first.next
first.next = second
second.next = n1

Once first.next is changed, it no longer leads to the old second node of the front half. Reading it afterwards makes second point at itself, so the front half's next node must be saved first.

06

Edge cases

One or two nodes

The middle loop never runs and the back half is at most one node, so the weave either does nothing or links 1 → 2, which is the same list. No early return is needed because the list is never empty.

Odd length, as in [1, 2, 3, 4, 5]

The front half is 1 → 2 → 3 and the back half reversed is 5 → 4. The weave stops after 4, and 3 stays at the end because the cut already ended it.

07

Complexity

Time
O(n)
Space
O(1)
Finding the middle, reversing about n / 2 nodes and weaving each touch every node a constant number of times. Only a few pointers are stored, because the nodes themselves are relinked.
08

Reorder List FAQ

Can I just copy the nodes into an array?

Yes. Put every node in an array, then link from both ends inward with two indexes and set the last node's next to null. It is short and correct in O(n) time, but it uses O(n) extra memory, which interviewers usually ask you to remove.

Why reverse the back half and not the front half?

The answer starts with the original head, which belongs to the front half. Reversing the back half leaves the head where it is and puts the old last node first in the other half, which is the node the weave needs second.