Group a linked list's odd-position nodes before its even-position nodes

Quick Overview

Reorder a singly linked list so the nodes at odd positions come first and the nodes at even positions follow, keeping the original order within each group. It tests in-place pointer relinking, loop conditions that stop safely, and edge cases such as empty and very short lists.

Group a linked list's odd-position nodes before its even-position nodes

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the `head` of a singly linked list, group all the nodes with odd indices together, followed by the nodes with even indices, and return the reordered list. The first node is considered odd, the second node is even, and so on. The relative order inside both the odd group and the even group should remain as it was in the input. ### Function Signature ```python class ListNode: def __init__(self, val: int = 0, next: "ListNode | None" = None): self.val = val self.next = next def odd_even_list(head: ListNode | None) -> ListNode | None: ``` ### Rules - Indices are 1-based positions in the list. Odd and even describe a node's position, so a node's value has no effect on its group. - The examples write a linked list as the list of its values from head to tail, and `[]` is the empty list. - Return the head of the reordered list. Its last node's `next` must be `None`. ### Constraints - The number of nodes in the linked list is in the range `[0, 10^4]`. - `-10^6 <= Node.val <= 10^6` ### Examples ```text Input: head = [1, 2, 3, 4, 5] Output: [1, 3, 5, 2, 4] ``` The nodes at positions 1, 3 and 5 hold 1, 3 and 5, and the nodes at positions 2 and 4 hold 2 and 4. The odd group comes first, then the even group, each in its original order. ```text Input: head = [2, 1, 3, 5, 6, 4, 7] Output: [2, 3, 6, 7, 1, 5, 4] ``` Positions 1, 3, 5 and 7 hold 2, 3, 6 and 7, and positions 2, 4 and 6 hold 1, 5 and 4. The grouping follows positions, so the odd value 1 at position 2 lands in the second group. ```text Input: head = [] Output: [] ``` An empty list has nothing to reorder.

Overview: Reorder a singly linked list so the nodes at odd positions come first and the nodes at even positions follow, keeping the original order within each group. It tests in-place pointer relinking, loop conditions that stop safely, and edge cases such as empty and very short lists.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the head of a singly linked list, group all the nodes with odd indices together, followed by the nodes with even indices, and return the reordered list.

The first node is considered odd, the second node is even, and so on.

The relative order inside both the odd group and the even group should remain as it was in the input.

Function Signature

class ListNode:
    def __init__(self, val: int = 0, next: "ListNode | None" = None):
        self.val = val
        self.next = next


def odd_even_list(head: ListNode | None) -> ListNode | None:

Rules

  • Indices are 1-based positions in the list. Odd and even describe a node's position, so a node's value has no effect on its group.
  • The examples write a linked list as the list of its values from head to tail, and [] is the empty list.
  • Return the head of the reordered list. Its last node's next must be None .

Constraints

  • The number of nodes in the linked list is in the range [0, 10^4] .
  • -10^6 <= Node.val <= 10^6

Examples

Input: head = [1, 2, 3, 4, 5]
Output: [1, 3, 5, 2, 4]

The nodes at positions 1, 3 and 5 hold 1, 3 and 5, and the nodes at positions 2 and 4 hold 2 and 4. The odd group comes first, then the even group, each in its original order.

Input: head = [2, 1, 3, 5, 6, 4, 7]
Output: [2, 3, 6, 7, 1, 5, 4]

Positions 1, 3, 5 and 7 hold 2, 3, 6 and 7, and positions 2, 4 and 6 hold 1, 5 and 4. The grouping follows positions, so the odd value 1 at position 2 lands in the second group.

Input: head = []
Output: []

An empty list has nothing to reorder.