Odd Even Linked List

Updated on 03 July, 2025
Odd Even Linked List header image

Problem Statement

When dealing with a singly linked list given by the head pointer, the task is to rearrange all nodes such that all nodes located at odd indices in the list are grouped together, followed by those at even indices. It's essential to note:

  • The index of the first node starts at 1, making it "odd," while the second node is "even," and the pattern continues alternately.
  • The nodes must retain their original relative ordering within their respective odd and even groups after grouping.
  • The solution must adhere to O(1) extra space usage and O(n) time complexity, ensuring efficiency in both space and processing time for potentially long linked lists.

Examples

Example 1

Input:

head = [1,2,3,4,5]

Output:

[1,3,5,2,4]

Example 2

Input:

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

Output:

[2,3,6,7,1,5,4]

Constraints

  • The number of nodes in the linked list is in the range [0, 104].
  • -106 <= Node.val <= 106

Approach and Intuition

To tackle this problem of group nodes into odd and even indexed sets, we can employ a two-pointer technique:

  1. Initialize two pointers: odd to start on the first node (head of the linked list), and even to start on the second node.
  2. Additionally, maintain a pointer evenHead that keeps a reference to the start of the even-index nodes.
  3. Traverse the linked list while adjusting the pointers:
    • Move the odd pointer through the list by jumping to the node two steps ahead (skipping the even indexed node).
    • Likewise, advance the even pointer by skipping over the next node, which will be odd indexed.
  4. This leapfrogging continues until either the odd or even pointer runs out of nodes to jump to (i.e., when they encounter the end of the list or a null node).
  5. Finally, connect the last node in the odd-indexed group (where odd.next would be null) to the evenHead, which marks the start of even-indexed nodes.

By the end of this process, all nodes that originally had odd indices will be grouped together at the beginning of the list followed by all nodes with even indices, all while maintaining their original order in their respective groups. The completion of these steps achieves the desired arrangement of the linked list in linear time and constant space, fulfilling the problem constraints and requirements efficiently.

Solutions

  • Java
java
public class Solution {
    public ListNode rearrangeNodes(ListNode head) {
        if (head == null) return null;
        ListNode oddNode = head, evenNode = head.next, startEvenNode = evenNode;
        while (evenNode != null && evenNode.next != null) {
            oddNode.next = evenNode.next;
            oddNode = oddNode.next;
            evenNode.next = oddNode.next;
            evenNode = evenNode.next;
        }
        oddNode.next = startEvenNode;
        return head;
    }
}

In the "Odd Even Linked List" problem, your primary goal is to rearrange the nodes in a singly linked list such that all the nodes at odd indices are grouped together followed by nodes at even indices.

Here's a straightforward implementation outline using Java:

  • Create two pointers, oddNode and evenNode, to manage and traverse through the odd and even indexed nodes respectively. Initialize oddNode to the first node (head) and evenNode to the second node (head.next).
  • Reserve the start of the even-indexed nodes with another pointer, startEvenNode, pointing to evenNode.
  • Iterate through the linked list utilizing these pointers:
    • Ensure moves are conducted to rearrange nodes: odd indexed nodes linked by oddNode and even indexed nodes by evenNode.
    • Progress the oddNode by skipping over the next node to connect directly to the next node of the current evenNode.
    • Similarly, update evenNode to point to the next of its current link.
  • Continue this process until you've traversed the entire list, ensuring at each step that the next node exists.
  • Finally, connect the last node in the odd-segment (terminated by oddNode) to the startEvenNode, effectively starting the even indexed segment.
  • Return the modified list starting from head.

By following this sequence, you reposition the nodes effectively, thus placing all odd nodes consecutively followed by even nodes, as required.

Comments

No comments yet.