Odd Even Linked List

Given the head of a singly linked list, group all odd-indexed nodes together followed by the even-indexed nodes, and return the reordered list (maintaining original relative order). Indexing is 1-based. Example: Input: head = [1,2,3,4,5] Output: [1,3,5,2,4] Explanation: Nodes at indices 1,3,5 come first, then 2,4. Pattern focus: Pointer Rewiring.

Input Format

head = ListNode

Output Format

return ListNode

Constraints

  • The number of nodes is in the range [0, 10^4].
  • -10^6 <= Node.val <= 10^6

Examples

Example 1:

Input:

head = [1,2,3,4,5]

Output:

[1,3,5,2,4]

Explanation:

Odd indices [1,3,5] then even [2,4].

Loading...
Odd Even Linked List - Linked List DSA Problem