Open In App

Merge Sort for Linked Lists in JavaScript

Last Updated : 30 Mar, 2023
Comments
Improve
Suggest changes
1 Like
Like
Report

Prerequisite: Merge Sort for Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible.

sorting image

In this post, Merge sort for linked list is implemented using JavaScript.

Examples:

Input : 5 -> 4 -> 3 -> 2 -> 1
Output :1 -> 2 -> 3 -> 4 -> 5

Input : 10 -> 20 -> 3 -> 2 -> 1
Output : 1 -> 2 -> 3 -> 10 -> 20

Implementation:

Output

10 -> 20 -> 3 -> 2 -> 1
After sorting : 1 -> 2 -> 3 -> 10 -> 20 

Complexity Analysis:

  • Time Complexity: O(n*log(n))
  • Auxiliary Space: O(n)

Next Article

Similar Reads