Odd nodes before even nodes
Mediumlinked list
Group the nodes at odd positions (1st, 3rd, …) first, followed by the nodes at even positions, keeping the order within each group. This is about a node's position, not its value. Use O(1) extra memory.
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]
Constraints
- 0 ≤ number of nodes ≤ 10⁴
- -10⁶ ≤ Node.val ≤ 10⁶
Submit checks your solution against 9 tests, including hidden ones.
// Provided by the judge: keep these definitions
class ListNode {
constructor(val = 0, next = null) {
this.val = val
this.next = next
}
}
/**
* @param {ListNode|null} head
* @return {ListNode|null}
*/
function oddEvenList(head) {
}