Given a linked list of size N containing all values from 1 to N. The task is to sort the linked list in increasing order.
Input : List = 3 -> 5 -> 4 -> 6 -> 1 -> 2 Output : 1 -> 2 -> 3 -> 4 -> 5 -> 6 Input : List = 5 -> 4 -> 3 -> 2 -> 1 Output : 1 -> 2 -> 3 -> 4 -> 5
Naive approach : The simplest approach is to sort this linked list with the use of any type of sorting method. It takes O(N*logN) minimum time.
Efficient approach : An efficient approach is to observe that the linked list contains a total of N elements and elements are numbered from 1 to N. Traverse the linked list and replace every element with its position.
Below is the implementation of this approach:
1 2 3 4 5 6
- Sort linked list which is already sorted on absolute values
- Ways to sort list of dictionaries by values in Python – Using itemgetter
- Sort a linked list of 0s, 1s and 2s
- Merge Sort for Doubly Linked List
- Sort a Linked List in wave form
- C Program for Bubble Sort on Linked List
- Sort a linked list of 0s, 1s and 2s by changing links
- Insertion Sort for Singly Linked List
- Iterative selection sort for linked list
- Bubble Sort On Doubly Linked List
- Iterative Merge Sort for Linked List
- Insertion Sort for Doubly Linked List
- Sort the biotonic doubly linked list | Set-2
- Sort the biotonic doubly linked list
- Sort a k sorted doubly linked list
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.