148. Sort List - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Given the head of a singly linked list, sort the list in ascending order and return the sorted list. You must do this in O(n log n) time and O(1) (only for bottom‑up) or O(log n) (recursive splits) extra space.

Examples

Input: 4 → 2 → 1 → 3
Output: 1 → 2 → 3 → 4
Explanation: sort and relink nodes.

Input: -1 → 5 → 3 → 4 → 0
Output: -1 → 0 → 3 → 4 → 5

Input: (empty list)
Output: (empty list)

Constraints

  • The number of nodes in the list is in the range [0, 5·10⁴].
  • -10⁵ ≤ Node.val ≤ 10⁵

Hints

  1. Can you sort by extracting values into an array, sorting them, then rebuilding the list?
  2. How would you split the list into two halves, sort each half recursively, then merge?

Approach 1 – Brute Force

Explanation

Take advantage of built‑in array sort:

  1. Traverse the list, copy all node values into a Python list / Java ArrayList.
  2. Sort that list of values.
  3. Create a new linked list by iterating sorted values and chaining new nodes.

This is easy but uses extra O(n) space.

Step‑by‑step Walkthrough

For 4 → 2 → 1 → 3:

  1. Extract → [4,2,1,3]
  2. Sort → [1,2,3,4]
  3. Build new list → 1 → 2 → 3 → 4

Python Implementation

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Approach 2 – Merge Sort (Optimal)

Explanation

Merge sort naturally fits linked lists:

  1. Split the list into two halves via fast/slow pointers.
  2. Recursively sort each half.
  3. Merge the two sorted halves in linear time.

This achieves O(n log n) time and only O(log n) recursion space.

Step‑by‑step Walkthrough

For 4 → 2 → 1 → 3:

  1. Find mid: slow at 2, fast at end → split into (4→2) and (1→3)
  2. Recurse on 4→2 → splits to (4) & (2), merges to 2→4
  3. Recurse on 1→3 → splits to (1) & (3), merges to 1→3
  4. Merge 2→4 and 1→3: compare heads → 1,2,3,4

Visual Example

[4→2→1→3]
    ↓ split
[4→2]    [1→3]
  ↓ split    ↓ split
[4] [2]    [1] [3]
  ↓ merge   ↓ merge
[2→4]      [1→3]
    ↓ merge final
[1→2→3→4]

Python Implementation

Python3
Python3

. . . .

Java Implementation

Java
Java

. . . .

Complexity Analysis

  • Approach 1 (array sort):
    • Time O(n log n) for sorting
    • Space O(n) for the array + new nodes
  • Approach 2 (merge sort):
    • Time O(n log n) for split & merge
    • Space O(log n) recursion stack (or O(1) if bottom‑up variant)

Common Mistakes

  • Forgetting to cut the list (prev.next = null) when splitting
  • Merging pointers incorrectly (losing rest of list)
  • Not handling the base case when head is null or single node
  • Using quick sort on a linked list leading to O(n²) worst‑case

Edge Cases

  • Empty list (head = null)
  • Single node list
  • All nodes have the same value
  • Already sorted or reverse‑sorted lists
  • Large lists to test recursion depth

Alternative Variations

  • Bottom‑Up Merge Sort to avoid recursion stack
  • Sort a k‑sorted list using a min‑heap in O(n log k)
  • Quick Sort on linked list (average O(n log n), worst O(n²))
  • Sort Doubly Linked List in place
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the difference between private and protected members of C++ classes?
What is the role of a frontend developer?
What does JSX stand for?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;