← DiffPush

Odd even LL

Standard Bar Linked List · Medium Problems of LL O(N) · O(1)

Re-sequencing the Badge Scanners by Turn Parity

A security line records badge scans in order, and the auditor wants all odd-numbered turns processed first followed by all even-numbered turns — positions keep their relative order within each group, and the value on each badge never changes. The controllers rewrite the links in place, threading two sub-sequences and then joining the tail of the first to the head of the second.

Input: An array head of node values representing the linked list.

Output: The values after regrouping: nodes from 1-based odd positions first, then even positions, relative order preserved within each group.

Constraints

Examples

Example 1

Input: {"head":[1,2,3,4,5]}
Output: [1,3,5,2,4]
Odd positions (1st, 3rd, 5th) lead in order, then the even ones follow.

Example 2

Input: {"head":[2,1,3,5,6,4,7]}
Output: [2,3,6,7,1,5,4]
Positions 1,3,5,7 hold 2,3,6,7; positions 2,4,6 hold 1,5,4.

Solve this in your browser →

Also on LeetCode ↗