Odd Even Linked List
Medium Linked List AI round
head is the head of a singly linked list, which may be empty. Its nodes are numbered from 1: the head is node 1, the node after it node 2, and so on.
Rearrange the list so that the odd-numbered nodes come first and the even-numbered nodes after them, each group in the order it had. It is the nodes' numbers that count, not their values.
Return the head of the rearranged list, or null if the list is empty.
Example 1
Nodes 1, 3 and 5 hold 4, -1 and 3; nodes 2, 4 and 6 hold 9, 6 and 8. The odd-numbered nodes come first, then the even-numbered, each group in its old order.
Example 2
Example 3
An empty list stays empty.
Constraints
0 ≤ number of nodes ≤ 10⁵-10⁹ ≤ node.val ≤ 10⁹
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Linked List
- Add Two NumbersAI round · Medium
- Merge Two Sorted ListsAI round · Easy
- Reverse Linked ListAI round · Very easy
- Swap Nodes in PairsAI round · Medium
- Convert Binary Search Tree to Sorted Doubly Linked ListMedium