VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sort-linked-list-0s-1s-2s-changing-links/

⇱ Sort a linked list of 0s, 1s and 2s - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sort a linked list of 0s, 1s and 2s

Last Updated : 2 Sep, 2025

Given a head of linked list containing nodes with values 0, 1, and 2 only, rearrange the list so that all 0s appear first, followed by all 1s, and then all 2s at the end, while maintaining the relative order within each group.

Examples:

Input:

👁 1

Output: 0 1 1 2 2 2 2 2
Explanation: All the 0s are segregated to the left end of the linked list, 2s to the right end of the list, and 1s in between.

👁 2-

Input:

👁 3-

Output: 0 1 2 2
Explanation: After arranging all the 0s, 1s and 2s in the given format, the output will be 0 -> 1 2 2.

👁 4

[Naive Approach] Using an Extra Array - O(n × log n) Time and O(n) Space

The idea is to first convert the linked list into an array to easily leverage sorting, as sorting an array is straightforward and efficient. After sorting the array, we traverse the linked list again and reassign the sorted values back to the nodes.


Output
0 -> 1 -> 1 -> 2 -> 2 -> 2 -> 2 -> 2

[Expected Approach - 1] Using Count of 0s, 1s and 2s - O(n) Time and O(1) Space

The idea is to traverse the linked list once and count the number of occurrences of 0s, 1s, and 2s. Once the counts are known, the linked list is traversed again, and the nodes are assigned the appropriate values based on the counts. First setting all nodes to 0, then to 1, and finally to 2.


Output
0 -> 1 -> 1 -> 2 -> 2 -> 2 -> 2 -> 2

[Expected Approach - 2] By Changing Links of Nodes - O(n) Time and O(1) Space

The idea is to 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.

  • If the current node's value is 0, append it after pointer zero and move pointer zero to current node.
  • If the current node's value is 1, append it after pointer one and move pointer one to current node.
  • If the current node's value is 2, append it after pointer two and move pointer two to current node.

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.


Output
0 -> 1 -> 1 -> 2 -> 2 -> 2 -> 2 -> 2

[Expected Approach - 3] Using Dutch National Flag Algorithm - O(n) Time and O(1) Space

The idea is to split the linked list into three separate sublists for 0s, 1s, and 2s using the Dutch National Flag algorithm. We maintain three dummy nodes and corresponding tail pointers to build each sublist during a single traversal. Once the segregation is done, we link these sublists in order: 0s -> 1s -> 2s. This avoids modifying node values and performs the operation in linear time and space.

Steps by step Implementation:

  • Create three dummy nodes to act as the start of separate lists for 0s, 1s, and 2s.
  • Initialize three tail pointers (zero, one, two) that point to the end of each of these sublists.
  • Traverse the original list and based on node value, append it to the respective sublist using tail pointers.
  • After appending a node, move the corresponding tail pointer forward to keep track of the last node.
  • Once traversal is complete, link zero list to one list and then link one list to two list carefully.
  • Ensure the last node of two list points to NULL to terminate the final merged list correctly.
  • Return the head of the combined list which starts from the next of zero dummy node.

Output
0 -> 1 -> 1 -> 2 -> 2 -> 2 -> 2 -> 2
Comment