Advertisement

Sort List - LeetCode 148 Solution

Sort List - Complete Solution Guide

Sort List is LeetCode problem 148, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the head of a linked list, return the list after sorting it in ascending order . Example 1: Input: head = [4,2,1,3] Output: [1,2,3,4] Example 2: Input: head = [-1,5,3,4,0] Output: [-1,0,3,4,5] Example 3: Input: head = [] Output: [] Constraints: The number of nodes in the list is in the range [0, 5 * 10 4 ] . -10 5 <= Node.val <= 10 5 Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?

Detailed Explanation

The problem asks us to sort a singly linked list in ascending order. The input is the `head` of the linked list, and the output should be the `head` of the sorted linked list. The problem specifies that the number of nodes in the list can range from 0 to 5 * 10<sup>4</sup>, and the value of each node can be between -10<sup>5</sup> and 10<sup>5</sup>. The follow-up question challenges us to achieve this in O(n log n) time and O(1) space, suggesting the use of Merge Sort.

Solution Approach

The provided solution uses the Merge Sort algorithm to sort the linked list. Merge Sort is a divide-and-conquer algorithm that recursively divides the list into smaller sublists until each sublist contains only one element (which is considered sorted). Then, it repeatedly merges the sublists to produce new sorted sublists until there is only one sorted list remaining. The algorithm consists of three main parts: splitting the list, recursively sorting the halves, and merging the sorted halves.

Step-by-Step Algorithm

  1. Step 1: Base Case: Check if the list is empty or has only one element. If so, it's already sorted, so return the head.
  2. Step 2: Split the List: Use the fast and slow pointer technique to find the middle of the list. The slow pointer moves one step at a time, and the fast pointer moves two steps at a time. When the fast pointer reaches the end, the slow pointer will be at the middle of the list. Disconnect the first half from the second half by setting slow.next to None.
  3. Step 3: Recursive Sorting: Recursively call `sortList` on both the first half (from head to slow) and the second half (from mid to the end).
  4. Step 4: Merge: Merge the two sorted halves using the `merge` function. This function creates a dummy node and iterates through both lists, appending the smaller value to the tail of the dummy node's list. Once one list is exhausted, append the remaining elements of the other list. Finally, return dummy.next, which is the head of the merged sorted list.

Key Insights

  • Insight 1: Merge Sort is an efficient sorting algorithm suitable for linked lists because it doesn't require random access, which is inefficient in linked lists.
  • Insight 2: Using the Fast/Slow pointer technique is crucial for efficiently finding the middle of the linked list without knowing its length beforehand.
  • Insight 3: Recursion is a natural way to implement the divide-and-conquer strategy of Merge Sort.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(log n)

Topics

This problem involves: Linked List, Two Pointers, Divide and Conquer, Sorting, Merge Sort.

Companies

Asked at: ByteDance, Lyft, Oracle, Palantir Technologies, TikTok, Yahoo.