Reorder List solution explained

Reorder List is solved in O(n) time and O(1) extra space with three linked-list steps: find the middle with a slow and a fast pointer, cut the list there and reverse the second half, then weave the two halves together by alternating one node from the front half with one from the reversed back half. The nodes are relinked in place; no values are copied.

By Published 7 min read

The task, restated

You get the head of a singly linked list. Rearrange the nodes so the first node is followed by the last, then the second, then the second to last, alternating inward until every node has been placed. Do it by changing next pointers, not by swapping the values inside nodes. Nothing is returned; the list is modified in place, starting from the same head.

Lists invented for this page; results produced by the tested code
Original orderReorderedNote
4, 9, 2, 7, 54, 5, 9, 7, 2Odd length: the middle node ends up last
4, 9, 2, 7, 5, 84, 8, 9, 5, 2, 7Even length
3, 8, 63, 6, 8Smallest case that changes
3, 83, 8Already in the target order
empty or one nodeunchangedNothing to do
Lists invented for this page; results produced by the tested code. Scroll sideways on a small screen.

Clarify whether you may use extra memory proportional to the list, since that decides whether the array approach is acceptable, and whether the interviewer will check that node identities are preserved rather than values copied. Most do expect real relinking.

What this problem measures

It is three classic linked-list routines glued together, so it tests fluency more than insight. Interviewers want to see that you can find a midpoint without counting, reverse a list without losing nodes, and splice two lists while keeping every pointer you still need. Since pointer bugs are easy to make and hard to see, how you draw and trace small cases is part of the evaluation.

Simple version: an array of nodes

Walk the list once and store every node in an array. Then use two indexes, one from the front and one from the back, and link front to back, back to the next front, and so on until they meet. Set the final node's next to nothing. That is O(n) time and O(n) space, and it is a good first answer because it is hard to get wrong.

class Node:
    def __init__(self, val, next=None):
        self.val = val
        self.next = next


def reorder_with_array(head):
    nodes = []
    while head:
        nodes.append(head)
        head = head.next
    i, j = 0, len(nodes) - 1
    while i < j:
        nodes[i].next = nodes[j]
        i += 1
        if i == j:
            break
        nodes[j].next = nodes[i]
        j -= 1
    if nodes:
        nodes[i].next = None  # i is where the two walks met: the new tail
Relink using an array of node references, O(n) space.

Constant space: middle, reverse, weave

  1. Find the end of the first halfMove slow one node and fast two nodes per step, stopping when fast cannot take two more steps. slow is then the last node of the first half; for odd lengths the first half gets the extra node.
  2. Cut and reverse the second halfRemember slow.next as the start of the second half and set slow.next to nothing, so the first half ends cleanly. Reverse the second half with the usual three-pointer loop.
  3. WeaveTake one node from the front half, then one from the reversed back half, and repeat until the back half runs out. The front half is the same length or one longer, so it absorbs any leftover node.
def reorder(head):
    if not head or not head.next:
        return
    # 1. find the end of the first half (slow stops at the middle)
    slow, fast = head, head
    while fast.next and fast.next.next:
        slow = slow.next
        fast = fast.next.next
    # 2. cut the list and reverse the second half
    second = slow.next
    slow.next = None
    prev = None
    while second:
        following = second.next
        second.next = prev
        prev = second
        second = following
    # 3. weave: one from the front half, one from the reversed back half
    first, second = head, prev
    while second:
        after_first, after_second = first.next, second.next
        first.next = second
        second.next = after_first
        first, second = after_first, after_second
Python: O(n) time, O(1) extra space. Uses the Node class above.
function reorder(head) {
  if (!head || !head.next) return;
  let slow = head;
  let fast = head;
  while (fast.next && fast.next.next) {
    slow = slow.next;
    fast = fast.next.next;
  }
  let second = slow.next;
  slow.next = null;
  let prev = null;
  while (second) {
    const next = second.next;
    second.next = prev;
    prev = second;
    second = next;
  }
  let first = head;
  second = prev;
  while (second) {
    const afterFirst = first.next;
    const afterSecond = second.next;
    first.next = second;
    second.next = afterFirst;
    first = afterFirst;
    second = afterSecond;
  }
}
JavaScript: nodes as { val, next } objects, same three steps.

Walking through five nodes

4, 9, 2, 7, 5
StepState after the step
Find the middleslow stops on 2, so the first half is 4, 9, 2
Cut and reverseFirst half 4, 9, 2; second half 7, 5 becomes 5, 7
Weave, round 14, 5, then the rest of the first half
Weave, round 24, 5, 9, 7, then 2
Back half emptyDone: 4, 5, 9, 7, 2, and 2's next is still empty from the cut
4, 9, 2, 7, 5. Scroll sideways on a small screen.

The cut in step 2 is what gives the final list a proper end. Without it, 2 would still point to 7, and after the weave 7 points to 2, which makes a loop. With the stopping rule fast.next and fast.next.next, the first half is never shorter than the second, so the weave loop only needs to watch the back half.

Cost

n is the number of nodes
ApproachTimeExtra space
Array of nodes, relink from both endsO(n)O(n)
Stack of second-half nodesO(n)O(n)
Middle, reverse, weaveO(n)O(1)
Repeatedly detach the tail and insert itO(n²)O(1)
n is the number of nodes. Scroll sideways on a small screen.

Edge cases

  • Empty list and a single node: return immediately.
  • Two nodes: the order does not change, but the code still runs one weave step, so check it does not create a loop.
  • Three nodes: the smallest case where the order changes.
  • Odd and even lengths, which differ in where the middle lands.
  • Repeated values, which make it impossible to check correctness by values alone. Track node identity in tests.

Pointer mistakes

  • Not cutting slow.next, which leaves a cycle that only shows up when you print the list.
  • Overwriting first.next before saving it, which loses the rest of the front half.
  • Using a midpoint rule that makes the second half longer, then looping on the front half and dropping a node.
  • Swapping values instead of relinking nodes, which interviewers usually rule out.
  • Returning a new head when the original head is still the head. The first node never moves.

How to explain it out loud

Fictional example

Breaking it into three known moves
Candidate
The order alternates between the front and the back, so if I had the back half reversed, this would just be a merge of two lists.
Candidate
So: slow and fast pointers to find the middle, cut there, reverse the second half, and then weave one node from each half. All three steps are linear and use a few pointers.
Interviewer
Why cut the list before reversing?
Candidate
If the first half still points into the second, the last node of the weave points back into the list and I get a cycle. Cutting gives the first half a proper end, and that node becomes the tail.

The weave is the same two-index merge used in the linear answer to Median of Two Sorted Arrays. Pointers that close in from both ends, here simulated by reversing the back half, appear in Valid Palindrome; the linked-list version of that question uses exactly this find-the-middle-and-reverse routine. For a different structure built on linked nodes, see LRU Cache. Browse by pattern on the coding problems hub.

Practicing linked lists with Natively

Linked-list rounds are won by careful tracing, so say each pointer change out loud as you make it. Natively, running on a Mac or Windows computer, transcribes those mock sessions, which lets you find the exact moment a pointer was overwritten in your reasoning. When you are practicing, it can read a list diagram or the prompt from a screenshot and suggest which routine applies. The AI assistant for coding interviews explains what it can do.

Questions people ask

How do you solve Reorder List?

Find the middle with slow and fast pointers, cut the list there, reverse the second half, and merge the halves by alternating one node from each. It runs in O(n) time with O(1) extra space.

Why do you need to cut the list at the middle?

Otherwise the first half's last node still points into the second half, and after the weave the list contains a cycle.

Can I use an array instead?

Yes. Store the nodes in an array and relink from both ends. It is O(n) time but O(n) extra space, a good first answer before the in-place version.

Does Reorder List allow swapping values?

Usually not. The intent is to relink nodes, so change next pointers rather than copying values between nodes.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP