Algo-Sim › Linked Lists

Sort List

Medium · Linked Lists · step through it in Java, Python and C++ on your own input

Merge sort on a linked list: split at the middle with slow/fast pointers, sort each half recursively, then merge the two sorted halves.

Problem

Sort the singly linked list starting at head in ascending order and return its new head.

Constraints

Edge cases to test

Follow-up questions

How do you get O(1) extra space?
Bottom-up merge sort: merge runs of size 1, 2, 4… iteratively instead of recursing. O(n log n) time, O(1) space.
Why merge sort for lists?
Merging needs no random access and no extra arrays; quicksort’s partitioning is awkward without indices.

Solution

Java

public ListNode sortList(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode slow = head, fast = head.next;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    ListNode mid = slow.next;
    slow.next = null;
    ListNode left = sortList(head);
    ListNode right = sortList(mid);
    ListNode dummy = new ListNode(0), tail = dummy;
    while (left != null && right != null) {
        if (left.val <= right.val) {
            tail.next = left;
            left = left.next;
        } else {
            tail.next = right;
            right = right.next;
        }
        tail = tail.next;
    }
    tail.next = (left != null) ? left : right;
    return dummy.next;
}

Python

def sort_list(head):
    if not head or not head.next: return head
    slow, fast = head, head.next
    while fast and fast.next:
        slow, fast = slow.next, fast.next.next
    mid, slow.next = slow.next, None
    left = sort_list(head)
    right = sort_list(mid)
    dummy = tail = ListNode()
    while left and right:
        if left.val <= right.val:
            tail.next, left = left, left.next
        else:
            tail.next, right = right, right.next
        tail = tail.next
    tail.next = left or right
    return dummy.next

C++

#include <bits/stdc++.h>
using namespace std;

class Solution {
  public:
    ListNode *sortList(ListNode *head) {
        if (head == nullptr || head->next == nullptr) return head;
        ListNode *slow = head, *fast = head->next;
        while (fast != nullptr && fast->next != nullptr) {
            slow = slow->next;
            fast = fast->next->next;
        }
        ListNode *mid = slow->next;
        slow->next = nullptr;
        ListNode *left = sortList(head);
        ListNode *right = sortList(mid);
        ListNode dummy, *tail = &dummy;
        while (left != nullptr && right != nullptr) {
            if (left->val <= right->val) {
                tail->next = left;
                left = left->next;
            } else {
                tail->next = right;
                right = right->next;
            }
            tail = tail->next;
        }
        tail->next = (left != nullptr) ? left : right;
        return dummy.next;
    }
};

More Linked Lists problems