Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Sort List (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a head of the linked list, return the list after sorting it in ascending order.

Examples

  1. Example 1:

    • Input: [3, 1, 2]
    • Expected Output: [1, 2, 3]
    • Justification: The list is sorted in ascending order, with 1 coming before 2, and 2 before 3.
  2. Example 2:

    • Input: [4]
    • Expected Output: [4]
    • Justification: A list with a single element is already sorted.
  3. Example 3:

    • Input: [9, 8, 7, 6, 5, 4, 3, 2, 1]
    • Expected Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    • Justification: The list is sorted in ascending order, with each element being smaller than the next.

Constraints:

  • The number of nodes in the list is in the range [0, 5 * 10<sup>4</sup>].
  • -10<sup>5</sup> <= Node.val <= 10<sup>5</sup>

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible