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
- Sort a Linked List in wave form
- Sort the biotonic doubly linked list
- C Program for Bubble Sort on Linked List
- Sort a linked list of 0s, 1s and 2s by changing links
- Sort a k sorted doubly linked list
- Insertion Sort for Doubly Linked List
- Bubble Sort On Doubly Linked List
- Merge Sort for Doubly Linked List
- Insertion Sort for Singly Linked List
- Iterative Merge Sort for Linked List
- Sort the biotonic doubly linked list | Set-2
- Sort the linked list in the order of elements appearing in the array
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.