23. Given a linked list of 0s, 1s and 2s, sort it.
The problem can be found at the following link: Question Link
My Approach
To segregate the linked list containing 0s, 1s, and 2s, I'll follow the following steps:
Traverse the linked list and count the occurrences of 0s, 1s, and 2s using a vector
cnt
.Traverse the linked list again and replace the node data based on the count of 0s, 1s, and 2s.
Time and Auxiliary Space Complexity
Time Complexity :
O(N)
, whereN
is the number of nodes in the linked list.Auxiliary Space Complexity :
O(1)
, as we are using a fixed-size vector of length 3 to store the count of elements.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated