Given a singly linked list of integers, the task is to sort it using iterative merge sort.
Merge Sort is often preferred for sorting a linked list. It is discussed here. However, the method discussed above uses Stack for storing recursion calls. This may consume a lot of memory if the linked list to be sorted is too large. Hence, a purely iterative method for Merge Sort with no recursive calls is discussed in this post.
We use bottom-up approach of Merge Sort in this post. We know that Merge Sort first merges two items, then 4 items and so on. The idea is to use an integer variable to store the gap to find the midpoint around which the linked list needs to be sorted. So the problem reduces to merging two sorted Linked List which is discussed here. However, we do not use an additional list to keep the merged list. Instead we merge the lists within itself. The gap is incremented exponentially by 2 in each iteration and the process is repeated.
1 2 3 4 5 6 7
Time Complexity : O(n Log n)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Merge a linked list into another linked list at alternate positions
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- C Program for Iterative Merge Sort
- Java Program for Iterative Merge Sort
- Python Program for Iterative Merge Sort
- Iterative Merge Sort
- Iterative selection sort for linked list
- Merge Sort with O(1) extra space merge and O(n lg n) time
- Merge Sort for Doubly Linked List
- Quick Sort vs Merge Sort
- Merge Sort vs. Insertion Sort
- Merge Sort for Linked Lists
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- Difference of two Linked Lists using Merge sort
- Find Length of a Linked List (Iterative and Recursive)
- Search an element in a Linked List (Iterative and Recursive)
- Print the alternate nodes of linked list (Iterative Method)
- Iterative approach for removing middle points in a linked list of line segements
- Check if linked list is sorted (Iterative and Recursive)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : andrew1234