05. Sort a Linked List of 0s, 1s and 2s
β GFG solution to Sort a Linked List of 0s, 1s and 2s problem: segregate nodes containing 0s, 1s, and 2s using optimal pointer manipulation technique. π
π§© Problem Description
π Examples
Example 1
Input: head = 1 β 2 β 2 β 1 β 2 β 0 β 2 β 2
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.Example 2
Input: head = 2 β 2 β 0 β 1
Output: 0 β 1 β 2 β 2
Explanation: After arranging all the 0s, 1s and 2s in the given format, the output will be sorted.π Constraints
β
My Approach
Node Segregation with Tail Pointers
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated