↓Skip to main content
  1. LeetCode/

LeetCode 328: Odd Even Linked List

·2 mins· ·
LeetCode Medium Linked-List
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-11-01

  • Submissions: Accepted (first try)

Approach
#

Split by position (not by value) into an odd-position list and an even-position list, keeping relative order, then append the even list after the odd list.

My Attempts
#

Accepted on the first try. I collected the two lists with two dummy heads and set the even list’s tail to None at the end (Linked List Template 1):

class Solution:
    def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        odd_dummy, even_dummy = ListNode(), ListNode()
        o, e = odd_dummy, even_dummy
        curr, i = head, 1
        while curr:
            if i % 2:
                o.next = curr
                o = curr
            else:
                e.next = curr
                e = curr
            curr = curr.next
            i += 1
        e.next = None
        o.next = even_dummy.next
        return odd_dummy.next

Solution: no dummy, two pointers leapfrogging
#

class Solution:
    def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head:
            return None
        odd, even = head, head.next
        even_head = even
        while even and even.next:
            odd.next = even.next
            odd = odd.next
            even.next = odd.next
            even = even.next
        odd.next = even_head
        return head

Complexity
#

Time O(n), extra space O(1).

Mistakes to Avoid
#

  • The last even node’s next may still point at an odd node; not cutting it creates a cycle.
  • Split by position, not by node value.

Interview Explanation
#

I chain the odd-position and even-position nodes into two lists in their original order, then link the odd list’s tail to the even list’s head and set the even tail to None. Only pointers change, so it is O(n) time and O(1) space.

Related

LeetCode 146: LRU Cache
·3 mins
LeetCode Medium Hash-Table Linked-List Design Doubly-Linked-List
LeetCode 146 note: a hash map finds nodes and a doubly linked list keeps the usage order; both get and updating an existing key must move the node to the front
LeetCode 92: Reverse Linked List II
·2 mins
LeetCode Medium Linked-List
LeetCode 92 note: walk a dummy to the node before left, then move the following nodes one at a time to just after it, right - left times
LeetCode 109: Convert Sorted List to Binary Search Tree
·2 mins
LeetCode Medium Linked-List Divide-and-Conquer Tree Binary-Search-Tree
LeetCode 109 note: take the middle found with fast and slow pointers as the root and recurse on both halves; the height balances naturally
LeetCode 116: Populating Next Right Pointers in Each Node
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search
LeetCode 116 note: in a perfect binary tree, walk each level through the next pointers already set on the level above: left.next = right and right.next = next.left, with O(1) extra space
LeetCode 117: Populating Next Right Pointers in Each Node II
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 117 note: connect next pointers level by level with BFS; for a non-perfect tree, looking only at the neighbour’s children breaks the chain
LeetCode 148: Sort List
·2 mins
LeetCode Medium Linked-List Two-Pointers Divide-and-Conquer Sorting
LeetCode 148 note: split in half with fast and slow pointers, sort each half recursively and merge; O(n log n) with no extra array