DSA linked lists learning path Level 3 This level · 2 of 3
Medium dsa linked-lists

Reorder List

The 30 second answer

The asked order is first, last, second, second last, and so on. I find the midpoint with slow and fast pointers, split the list, reverse the second half, and then merge the two halves by alternating nodes. The first half is the longer one when the length is odd, so the leftover middle node stays in place.

Could you say this in an interview?

Problem Reorder a list so that the nodes go first, last, second, second last, and so on, in place. For 1 -> 2 -> 3 -> 4 the result is 1 -> 4 -> 2 -> 3; for 1 -> 2 -> 3 -> 4 -> 5 it is 1 -> 5 -> 2 -> 4 -> 3. Original: LeetCode 143: Reorder List.

Understanding the prompt You must change the list in place and may not allocate a new list of nodes, although a temporary stack of pointers is a common first idea. The method returns void; the head stays the same object.

Pattern recognition A combination of finding the middle, reversing a list, and merging two lists, which are the three building blocks already used above.

Brute force Copy the nodes into an array and then rewire i with n-1-i: O(n) extra space.

Optimal Find the middle with slow and fast starting together; when fast can no longer take two steps, slow.next is the start of the second half. Cut slow.next, reverse that second half, then weave: while the second half remains, insert its current node after the current first-half node and advance both. For an odd length the middle stays at the end of the first half.

Alternatives Push every node onto a stack, then pop and insert after each walked node, stopping at the midpoint. That is easier to write and uses O(n) space, which some interviewers will accept as a first version.

Java solution

public class ReorderList {

    static class ListNode {
        int val;
        ListNode next;
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    static ListNode list(int... a) {
        var dummy = new ListNode(0);
        var t = dummy;
        for (int x : a) {
            t.next = new ListNode(x);
            t = t.next;
        }
        return dummy.next;
    }

    static String str(ListNode h) {
        var sb = new StringBuilder("[");
        for (ListNode p = h; p != null; p = p.next) {
            if (sb.length() > 1) sb.append(", ");
            sb.append(p.val);
        }
        return sb.append("]").toString();
    }

    static void reorderList(ListNode head) {
        if (head == null || head.next == null) return;
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode second = reverse(slow.next);
        slow.next = null;
        ListNode first = head;
        while (second != null) {
            ListNode n1 = first.next, n2 = second.next;
            first.next = second;
            second.next = n1;
            first = n1;
            second = n2;
        }
    }

    static ListNode reverse(ListNode head) {
        ListNode prev = null, cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }

    public static void main(String[] args) {
        var a = list(1, 2, 3, 4);
        reorderList(a);
        System.out.println(str(a)); // [1, 4, 2, 3]
        var b = list(1, 2, 3, 4, 5);
        reorderList(b);
        System.out.println(str(b)); // [1, 5, 2, 4, 3]
    }
}

Complexity O(n) time, O(1) extra space.

Edge cases

  • Official samples: [1,2,3,4] becomes [1,4,2,3]; [1,2,3,4,5] becomes [1,5,2,4,3].
  • Two nodes swap into the same two nodes.
  • A single node is unchanged.

Follow-ups

  • Reverse the first half instead and merge: it is messier to keep the head. Sticking to 'reverse the second half' is cleaner.
  • Palindrome Linked List uses the same mid-and-reverse preparation.

Tests [1,2,3,4] becomes [1,4,2,3]. [1,2,3,4,5] becomes [1,5,2,4,3].

How to say it in the interview "I find the middle, split the list, reverse the second half, and then merge by alternating nodes from the two halves. The first half keeps the middle node when the length is odd. Linear time, constant extra space."

Sources (1)
  1. LeetCode 143: Reorder Listleetcode.com
Previous
Next