Rearrange Product Bundles by Parity
At Nykaa, efficient processing of product bundles is crucial for inventory management and order fulfillment. Sometimes, bundles need to be processed in a specific order based on their unique identifiers. This problem simulates a scenario where product bundles, identified by numerical IDs, need to be re-sorted to prioritize bundles with even IDs over those with odd IDs, while maintaining their original relative sequence within their respective parity groups.
You are given the head of a singly linked list, where each node contains a unique positive integer representing a product bundle ID. Your task is to rearrange the linked list such that:
You should return the head of the rearranged linked list.
head: The head of a singly linked list. Each ListNode contains a positive integer val representing a product bundle ID.head is null).val) are positive integers.next pointers; do not create new nodes for the values.Examples
Example 1
Input:
head = [1,2,3,4,5]
Output:
[2,4,1,3,5]
Note:
Even IDs (2, 4) maintain relative order and come first. Odd IDs (1, 3, 5) maintain relative order and come after.
Example 2
Input:
head = [2,4,6]
Output:
[2,4,6]
Note:
All IDs are even, so their relative order is preserved and no rearrangement is needed.
Example 3
Input:
head = [1,3,5]
Output:
[1,3,5]
Note:
All IDs are odd, so their relative order is preserved and no rearrangement is needed.
Example 4
Input:
head = []
Output:
[]
Note:
An empty list remains empty.
Constraints
0 and 10^4.val (bundle ID) is an integer between 1 and 10^9.Loading editor...