Reorder List (LeetCode 143): Solution and Explanation

Medium
Linked List
Two Pointers
Stack
Recursion

You get the head of a singly linked list and must rearrange its nodes so the order alternates between the front and the back: first node, last node, second node, second to last, and so on. The change happens in place by relinking nodes, not by swapping values or returning a new list.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

The simplest idea is to copy every node into an array. With random access you can walk two indices toward each other, one from the front and one from the back, and relink the nodes in the alternating order. That works in O(n) time but uses O(n) extra memory, and the whole point of a linked list question like this is usually to see whether you can avoid that.

Look at the target order more carefully. The nodes taken from the front appear in their original order: L0, L1, L2. The nodes taken from the back appear in reverse order: Ln, Ln-1, Ln-2. So the answer is really two lists woven together, the first half as it is and the second half reversed. Once you see that, the problem breaks into three standard linked list moves you already know.

First, find the middle with slow and fast pointers, so you can cut the list into two halves without counting its length. Second, reverse the second half in place with the usual three-pointer reversal. Third, merge the two halves by taking one node from each in turn. Every step is a single linear pass that only rearranges next pointers, which gives O(n) time and constant extra space.

Approach: Find middle, reverse second half, merge

  1. If the list is empty, return immediately because there is nothing to reorder.
  2. Start slow at head and fast at head.next, then advance slow by one and fast by two while fast and fast.next exist. When the loop ends, slow sits at the last node of the first half.
  3. Set second = slow.next and cut the list by setting slow.next = None, so the first half now ends cleanly.
  4. Reverse the second half by walking it with prev and tmp, pointing each node back at the previous one. When done, prev is the head of the reversed half.
  5. Merge by setting first = head and second = prev. Save first.next and second.next, link first to second and second to the saved next of first, then advance both pointers.
  6. Stop when second runs out. The second half is never longer than the first, so the leftover node of an odd-length list is already in the right spot at the end.

Step-by-step example

head = [1,2,3,4,5]
  1. Start with slow at 1 and fast at 2. After one move, slow is 2 and fast is 4. After another, slow is 3 and fast is None, so the loop stops.
  2. The first half is 1 -> 2 -> 3. Cutting at slow gives a second half of 4 -> 5.
  3. Reversing the second half produces 5 -> 4, with prev pointing at 5.
  4. Merge round one: link 1 to 5 and 5 to 2. Now first is 2 and second is 4.
  5. Merge round two: link 2 to 4 and 4 to 3. Now second is None, so the loop ends.
  6. The list reads 1 -> 5 -> 2 -> 4 -> 3, and node 3 already ends the list because its next was cleared during the cut.

Code: Python, Java, C++

1class Solution:
2    def reorderList(self, head: Optional[ListNode]) -> None:
3        if not head: return
4        
5        # Find middle
6        slow, fast = head, head.next
7        while fast and fast.next:
8            slow = slow.next
9            fast = fast.next.next
10            
11        # Reverse second half
12        second = slow.next
13        prev = slow.next = None
14        while second:
15            tmp = second.next
16            second.next = prev
17            prev = second
18            second = tmp
19            
20        # Merge two halves
21        first, second = head, prev
22        while second:
23            tmp1, tmp2 = first.next, second.next
24            first.next = second
25            second.next = tmp1
26            first, second = tmp1, tmp2

Time and space complexity

Time

O(n)

Finding the middle visits about half the nodes, reversing touches the second half once, and merging touches every node once. Three linear passes add up to O(n).

Space

O(1)

Only a handful of pointer variables are used. The nodes are relinked in place and no array, stack or recursion is involved.

Other approaches

Array of nodes with two indices (time O(n), space O(n))

Store every node in an array, then use a left index and a right index moving toward each other to relink the nodes in alternating order, finally setting the last node's next to None. It is easier to get right under pressure but costs linear memory.

Stack for the back half (time O(n), space O(n))

Push all nodes onto a stack, then walk from the head and pop from the stack to get nodes from the back, inserting each one after the current front node until the pointers meet. It avoids the explicit reversal but still uses O(n) space.

Common mistakes

  • Forgetting to cut the list with slow.next = None before reversing. Without the cut, the first half still points into the second half and the merge creates a cycle.
  • Starting fast at head instead of head.next changes where slow stops. On even-length lists that puts the extra node in the first half's wrong position unless you adjust the rest of the code to match.
  • Losing references during the merge. You have to save both first.next and second.next before rewiring, or you lose the rest of one list.
  • Looping the merge on first instead of second. The first half can be one node longer, so the merge must stop when the reversed half is exhausted.

Frequently asked questions

Why does Reorder List need to reverse the second half?

The nodes from the back of the list must be visited from last to first, but a singly linked list only lets you move forward. Reversing the second half turns that backward walk into a forward one, so a simple two-pointer merge can produce the alternating order.

Can Reorder List be solved in O(1) space?

Yes. The three-step approach of finding the middle, reversing the second half and merging only rearranges pointers, so it uses constant extra memory. The array and stack approaches are simpler but cost O(n) space.

What happens with an odd number of nodes?

The first half keeps the middle node, so it is one node longer than the second half. The merge stops when the second half runs out, and the middle node naturally ends up last, which is exactly where it belongs.

Why is the function return type None?

The problem asks you to modify the list in place. The caller still holds the original head, which stays the first node, so there is nothing new to return.