The above solution does not work when these values have associated data with them. For example, these three represent three colors and different types of objects associated with the colors and we want to sort objects (connected with a linked list) based on colors.
In this post, a new solution is discussed that works by changing links.
Iterate through the linked list. Maintain 3 pointers named zero, one and two to point to current ending nodes of linked lists containing 0, 1, and 2 respectively. For every traversed node, we attach it to the end of its corresponding list. Finally we link all three lists. To avoid many null checks, we use three dummy pointers zeroD, oneD and twoD that work as dummy headers of three lists.
Linked List Before Sorting
1 2 0 1
Linked List After Sorting
0 1 1 2
Thanks to Musarrat_123 for suggesting above solution in a comment here.
Time Complexity: O(n) where n is number of nodes in linked list. Auxiliary Space: O(1)
This article is contributed by Bhaskar Kumar Mishra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.