Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Odd Even Linked List (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given the head of a singly linked list, rearrange the nodes such that all nodes with odd indices are grouped together followed by all nodes with even indices, and return the updated list

The order of the nodes within the odd and even groups should remain as in the original list.

The reordering should be done in-place with O(1) extra space and O(n) time complexity.

Examples

Example 1:

  • Input: head = [10, 20, 30, 40, 50, 60]
  • Expected Output: [10, 30, 50, 20, 40, 60]
  • Justification: Nodes at indices 1, 3, and 5 are grouped together first (10, 30, 50), followed by nodes at indices 2, 4, and 6 (20, 40, 60).

Example 2:

  • Input: head = [1, 3, 5, 7, 9]
  • Expected Output: [1, 5, 9, 3, 7]
  • Justification: Nodes at indices 1, 3, and 5 are grouped together first (1, 5, 9), followed by nodes at indices 2 and 4 (3, 7).

Example 3:

  • Input: head = [2, 4, 6, 8, 10, 12, 14]
  • Expected Output: [2, 6, 10, 14, 4, 8, 12]
  • Justification: Nodes at indices 1, 3, 5, and 7 are grouped together first (2, 6, 10, 14), followed by nodes at indices 2, 4, and 6 (4, 8, 12).

Constraints:

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

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible