Search⌘ K
AI Features

Solution: Odd Even Linked List

Explore how to rearrange nodes in a singly linked list by grouping all odd-indexed nodes followed by even-indexed nodes while preserving their original order. Understand the in-place algorithm that uses constant extra space and linear time, mastering pointer manipulation techniques for efficient linked list problems.

Statement

Given the head of a singly linked list, rearrange the nodes so that all nodes at odd indexes appear first, followed by all nodes at even indexes.

  • The first node in the list is considered at odd index 1, the second at even index 2, and so on.

  • Within the odd group and the even group, the relative order of the nodes must remain the same as in the original list.

Return the head of the reordered linked list.

Note: You must solve the problem in  ...