378. Sort a Linked List of 0s, 1s and 2s by Changing Links

MediumLinked ListLinked ListPointers

Given a linked list of nodes whose values are only 0, 1, or 2, sort it in ascending order by rearranging the node links (not just swapping values). Return the resulting list as an array.

Input: An array of 0/1/2 values.

Output: Array — the sorted list.

Examples

Example 1
Input: [2,1,2,1,0,2,0]
Output: [0,0,1,1,2,2,2]
Explanation: Nodes regrouped by value.
Example 2
Input: [0,0,0]
Output: [0,0,0]
Explanation: All zeros.
Example 3
Input: [2,1,0]
Output: [0,1,2]
Explanation: Sorted.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →