Problem
You get the head of a singly linked list. In a singly linked list, each node points to the next node. The first node is at position 1, the second node is at position 2, and so on. The positions count the nodes, not the values.
Change the order of the nodes so that all nodes at odd positions come first. All nodes at even positions come after them. Keep the order of the nodes inside each group. Return the head of the changed list.
Use O(1) extra memory and O(n) time. Change the links between the nodes, and do not make new nodes.
Example 1
- Input:
- head = [7,3,9,1,5]
- Output:
- [7,9,5,3,1]
- Explanation:
- The nodes at odd positions have the values 7, 9 and 5. The nodes at even positions have the values 3 and 1.
Example 2
- Input:
- head = [10,20,30,40,50,60]
- Output:
- [10,30,50,20,40,60]
Example 3
- Input:
- head = [8,2]
- Output:
- [8,2]
- Explanation:
- Node 8 is at position 1 and node 2 is at position 2, so the order does not change.
Constraints
- 0 <= number of nodes <= 10^4
- -10^6 <= Node.val <= 10^6
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 328. Odd Even Linked List
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source