368. Segregate 0s, 1s, and 2s in a Linked List

MediumLinked ListCountingLinked List

Given a linked list whose nodes contain only the values 0, 1, or 2, rearrange the list so that all 0s come first, then all 1s, then all 2s. Return the result as an array.

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

Output: Array — the segregated list.

Examples

Example 1
Input: [1,2,2,1,2,0,2,2]
Output: [0,1,1,2,2,2,2,2]
Explanation: Grouped by value.
Example 2
Input: [2,2,1,1,0,0]
Output: [0,0,1,1,2,2]
Explanation: Sorted into groups.
Example 3
Input: [0,0,0]
Output: [0,0,0]
Explanation: All zeros.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →