372. Rearrange Linked List — Negatives Before Positives

MediumLinked ListLinked ListTwo Pointers

Given a linked list of integers, rearrange it so that all negative values appear before all non-negative values (zero counts as non-negative), preserving the relative order within each group. Return the result as an array.

Input: An array of node values.

Output: Array — negatives first, then non-negatives.

Examples

Example 1
Input: [1,-2,3,-4,5]
Output: [-2,-4,1,3,5]
Explanation: Negatives first, order preserved.
Example 2
Input: [-1,-2,-3]
Output: [-1,-2,-3]
Explanation: All negative.
Example 3
Input: [1,2,3]
Output: [1,2,3]
Explanation: No negatives.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →