Problem Statement:
Sort the given singly linked list in ascending order. You have to do it in-place (using only constant extra space).
Input Format:
There is only one argument named head, denoting the head of the given singly linked list.
Output Format:
After sorting, return the head of the same linked list that is provided in the input.
Constraints:
Sample Test Case 1:
Sample Input 1:
1 -> 7 -> 4 -> 2 -> NULL
Sample Output 1:
1 -> 2 -> 4 -> 7 -> NULL
Sample Test Case 2:
Sample Input 2:
3 -> 2 -> 1 -> 5 -> 4 -> NULL
Sample Output 2:
1 -> 2 -> 3 -> 4 -> 5 -> NULL
Here we will sort the singly linked list using the insertion sort algorithm. Following is the Insertion sort algorithm for a linked list:
Example:
List: 3 -> 2 -> 1-> 5 -> 4 -> NULL
Step 1:
sortedListHead: NULL
current List: 3 -> 2 -> 1-> 5 -> 4 -> NULL
Step 2:
sortedListHead: 3 -> NULL
current List: 2 -> 1 -> 5 -> 4 -> NULL
Step 3:
sortedListHead: 2 -> 3 -> NULL
current List: 1 -> 5 -> 4 -> NULL
Step 4:
sortedListHead: 1 -> 2 -> 3 -> NULL
current List: 5 -> 4 -> NULL
Step 5:
sortedListHead: 1 -> 2 -> 3 -> 5 -> NULL
current List: 4 -> NULL
Step 6:
sortedListHead: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
current List: NULL
Step 7:
Return sortedListHead.
If we are given a sorted linked list in increasing order, each step will take O(n) time to insert the value in sortedListHead, where n is the length of the given linked list. This is the worst case of this solution.
Time Complexity:
O(n^2), where n is the length of the given singly linked list
As per the example, the number of steps will be n, and in the worst case, each step will take O(n) time to insert the value in the sorted list. So, in the worst case, the time complexity will be O(n^2).
Auxiliary Space Used:
O(1)
As we are placing the linked list node in its correct position to sort it in increasing order, we are not using any extra space.
Space Complexity:
O(n)
As input is O(n) and auxiliary space used is O(1). So, O(n) + O(1) -> O(n).
<Code snippet 1>
Here, we will sort the singly linked list using merge sort. Following is the merge sort algorithm for linked lists:
MergeSort(head):
There are two pointers to get the middle element of the linked list — fast pointer and slow pointer. At the start, the slow pointer points to the first element of the linked list, and the fast pointer points to the second element of the linked list.
Then the fast pointer moves two positions ahead, and the slow pointer moves one position. When the fast pointer reaches the end of the linked list, the slow pointer will point to the middle element of the linked list.
Example:
List : 3 -> 2 -> 1-> 5 -> 4 -> NULL
Time Complexity:
O(n * logn)
Auxiliary Space Used:
O(logn)
Due to the space used by recursive calls to the function sortLinkedList.
Space Complexity:
O(n)
The input is O(n) and auxiliary space used is O(logn). Therefore, O(n) + O(logn) -> O(n).
Problem Statement:
Sort the given singly linked list in ascending order. You have to do it in-place (using only constant extra space).
Input Format:
There is only one argument named head, denoting the head of the given singly linked list.
Output Format:
After sorting, return the head of the same linked list that is provided in the input.
Constraints:
Sample Test Case 1:
Sample Input 1:
1 -> 7 -> 4 -> 2 -> NULL
Sample Output 1:
1 -> 2 -> 4 -> 7 -> NULL
Sample Test Case 2:
Sample Input 2:
3 -> 2 -> 1 -> 5 -> 4 -> NULL
Sample Output 2:
1 -> 2 -> 3 -> 4 -> 5 -> NULL
Here we will sort the singly linked list using the insertion sort algorithm. Following is the Insertion sort algorithm for a linked list:
Example:
List: 3 -> 2 -> 1-> 5 -> 4 -> NULL
Step 1:
sortedListHead: NULL
current List: 3 -> 2 -> 1-> 5 -> 4 -> NULL
Step 2:
sortedListHead: 3 -> NULL
current List: 2 -> 1 -> 5 -> 4 -> NULL
Step 3:
sortedListHead: 2 -> 3 -> NULL
current List: 1 -> 5 -> 4 -> NULL
Step 4:
sortedListHead: 1 -> 2 -> 3 -> NULL
current List: 5 -> 4 -> NULL
Step 5:
sortedListHead: 1 -> 2 -> 3 -> 5 -> NULL
current List: 4 -> NULL
Step 6:
sortedListHead: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
current List: NULL
Step 7:
Return sortedListHead.
If we are given a sorted linked list in increasing order, each step will take O(n) time to insert the value in sortedListHead, where n is the length of the given linked list. This is the worst case of this solution.
Time Complexity:
O(n^2), where n is the length of the given singly linked list
As per the example, the number of steps will be n, and in the worst case, each step will take O(n) time to insert the value in the sorted list. So, in the worst case, the time complexity will be O(n^2).
Auxiliary Space Used:
O(1)
As we are placing the linked list node in its correct position to sort it in increasing order, we are not using any extra space.
Space Complexity:
O(n)
As input is O(n) and auxiliary space used is O(1). So, O(n) + O(1) -> O(n).
<Code snippet 1>
Here, we will sort the singly linked list using merge sort. Following is the merge sort algorithm for linked lists:
MergeSort(head):
There are two pointers to get the middle element of the linked list — fast pointer and slow pointer. At the start, the slow pointer points to the first element of the linked list, and the fast pointer points to the second element of the linked list.
Then the fast pointer moves two positions ahead, and the slow pointer moves one position. When the fast pointer reaches the end of the linked list, the slow pointer will point to the middle element of the linked list.
Example:
List : 3 -> 2 -> 1-> 5 -> 4 -> NULL
Time Complexity:
O(n * logn)
Auxiliary Space Used:
O(logn)
Due to the space used by recursive calls to the function sortLinkedList.
Space Complexity:
O(n)
The input is O(n) and auxiliary space used is O(logn). Therefore, O(n) + O(logn) -> O(n).
Attend our free webinar to amp up your career and get the salary you deserve.