Linked List· 25 min
Odd Even Linked List
LeetCode #328 · Medium · Interactive step-by-step visualization
Setup
Example input
head = [1, 2, 3, 4, 5]Step 1 / 10
Current step
1 / 10Group by position, not by value. Odd and even refer to a node's place in the list, so the two groups interleave perfectly. Weave two chains in place, then join them.
Step log
head
headnull
1
2
3
4
5
list
1 → 2 → 3 → 4 → 5
Named pointer
Active node
Committed or completed
Detached or removed
odd-even-linked-list.java
1public ListNode oddEvenList(ListNode head) {2 if (head == null || head.next == null) return head;3 ListNode odd = head, even = head.next, evenHead = even;4 while (even != null && even.next != null) {5 odd.next = even.next;6 odd = odd.next;7 even.next = odd.next;8 even = even.next;9 }10 odd.next = evenHead;11 return head;12}● Java
Time
O(n)
Space
O(1)
Pattern
linked list
Frames
10
