Medium

Quiz

#328 Odd Even Linked List

APPROACH

Given the head of a singly linked list, rearrange the nodes so all odd-indexed nodes appear before all even-indexed nodes, preserving the original relative order within each group. Index numbering starts at 1 (first node is odd, second is even, etc.).

Achieve this in O(1) extra space and O(n) time.

Example 1:

Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]

Example 2:

Input: head = [2,1,3,5,6,4,7]
Output: [2,3,6,7,1,5,4]
1 of 4
1:00

What is the optimal approach for this problem?