Merge Sort Linked List

Moderate
0/80
Average time to solve is 10m
profile
Contributed by
42 upvotes
Asked in companies
AdobeGoogleSamsung R&D Institute

Problem statement

You are given a Singly Linked List of integers. Sort the Linked List using merge sort.

Merge Sort is a Divide and Conquer algorithm. It divides the input into two halves, calls itself for the two halves, and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, L, M, R) is a key process that assumes that arr[L..M] and arr[M + 1...R] are sorted and merges the two sorted subarrays into one.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 
Then the T test cases follow.

The first line of each test case contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
Output Format:
For each test case, print the sorted linked list. The elements of the sorted list should be single-space separated, terminated by -1.

The output of each test case is printed in a separate line.
Note:
You are not required to print the output, it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 10
1 <= N <= 10^4
-10^9 <= data <= 10^9 and data != -1

Where data is the value associated with the node.

Time Limit: 1 sec
Sample Input 1:
3
1 -2 3 -1
9 9 -1
4 -1
Sample Output 1:
-2 1 3 -1
9 9 -1
4 -1
Explanation For Sample Output 1:
For every test case, the sorted list is printed, terminated by -1.
Sample Input 2:
2
1 1 1 -1
3 -3 -1
Sample Output 2:
1 1 1 -1
-3 3 -1
Hint

Think of the same way, you would sort an array using MergeSort, but for the linked list, we will change pointers rather than swapping data.

Approaches (1)
Merge Sort

We will use the ‘Merge Sort’ algorithm to sort the given linked list. Merge Sort is a Divide and Conquer algorithm. In this algorithm, we will divide the list into two parts, recursively sort the two parts and finally merge them such that the resultant list will be sorted.

 

Algorithm:

 

  1. If the list contains only one node, return the head of the list.
  2. Else, divide the list into two sublists. For this, we will take pointers ‘MID’ and ‘TAIL’ which will initially point to the head node. We will change ‘MID’ and ‘TAIL’ as follows until ‘TAIL’ becomes NULL:
    ‘MID’ = ‘MID’ -> next
    ‘TAIL’ = ‘TAIL’ -> next -> next.

The above operation will ensure that ‘MID’ will point to the middle node of the list. ‘MID’ will be the head of the second sublist, so we will change the ‘NEXT’ value of the node which is before mid to NULL.

 

  1. Call the same algorithm for the two sublists.
  2. Merge the two sublists and return the head of the merged list. For this, we will take a pointer to a node, ‘MERGE_LIST’, which will be initially pointing to NULL. If the head of the first sublist has a value less than the head of the second sublist then we will point the  ‘MERGE_LIST’ to the head of the first sublist and change the head to its next value. Else, we will point the ‘MERGE_LIST’ to the head of the second sub list.
  3. In the end, if any of the sublists becomes empty and the other sublist is non-empty, then we will add all nodes of the non-empty sublist in the 'MERGE_LIST’.
Time Complexity

O(N * log2(N)), Where ‘N’ is the number of nodes in the linked list.

 

The algorithm used (Merge Sort) is divide and conquer. So, for each traversal, we divide the list in 2 parts, thus there are log2(N) levels and the final complexity is O(N * log2(N)).

Space Complexity

O(log2(N)), where ‘N’ is the number of nodes in the linked list.

 

Since the algorithm is recursive and there are log2(N) levels, it requires O(log2(N)) stack space.

Code Solution
(100% EXP penalty)
Merge Sort Linked List
Full screen
Console