Odd Even Linked List

To solve this coding challenge, we need to reorder the nodes in a singly linked list such that all nodes with odd indices come first, followed by nodes with even indices, while maintaining the relative order within the groups. The process should meet O(1) space complexity and O(n) time complexity requirements. Let’s break down the problem and provide a highly detailed explanation and pseudocode.

Explanation

The challenge is to reorder the nodes of a singly linked list where nodes indexed by odd numbers come before nodes indexed by even numbers. It is important to understand the constraints and requirements:
  1. The problem should be solved using O(1) extra space, which means we should not use extra data structures like arrays, lists, or any form of auxiliary storage that consumes space proportional to the input size.
  2. The time complexity should be O(n), where
    n
    is the number of nodes in the linked list. This implies that each node should be processed at most once.
  3. Strategy:
    To solve the problem, we can utilize two pointers technique:
  4. Odd Pointer: This pointer traverses and links all nodes at odd indices.
  5. Even Pointer: This pointer traverses and links all nodes at even indices.
  6. We use an initial splitting process where each pointer starts from the respective indexed nodes, and then link the pointers as they traverse the list. The specific steps are:
  7. Initialization: Set two pointers,
    odd
    and
    even
    . Initially,
    odd
    points to the head of the list (first node) and
    even
    points to the second node. We also initialize
    even_head
    to keep the start of the even indexed nodes, so as to connect the end of the odd indexed node list with the start of the even indexed list later.
  8. Traversal:
    • Iterate through the list, adjusting the
      next
      pointers of odd and even nodes accordingly.
    • During each iteration, link the current odd node to the next odd node and the current even node to the next even node.
    • Move the
      odd
      and
      even
      pointers forward in each step.
  9. Reconnection: After reaching the end of the list (when there are no more nodes to process), connect the last odd node to the first even node captured by
    even_head
    .
  10. This approach maintains in-place reordering and does not use extra space aside from a few pointers.
    Detailed Steps in Pseudocode:
  11. Handle edge cases:
    • If the head is
      None
      , which means the list is empty, return
      None
      .
  12. Initialize pointers:
    • odd
      to point to
      head
      .
    • even
      to point to
      head.next
      .
    • even_head
      to store the starting point of the even list.
  13. Perform Reordering:
    • Loop while
      even
      and
      even.next
      are not
      None
      .
    • Move
      odd.next
      to
      even.next
      to get the next odd indexed node.
    • Move
      odd
      to
      odd.next
      to update the odd pointer.
    • Move
      even.next
      to
      odd.next
      for the next even indexed node.
    • Move
      even
      to
      even.next
      to update the even pointer.
    • After the loop, connect the end of the odd list to
      even_head
      .
  14. Return the modified list:
    • The
      head
      now points to the start of the reordered list.
Pseudocode:
                                            
# Define Linked List Node (for context, not actual pseudocode as per instructions)
# class ListNode:
#     def __init__(self, value=0, next=None):
#         self.value = value
#         self.next = next

# Function to reorder the linked list
function reorderOddEvenLinkedList(head):
    # Edge case: if the head is None, return None
    if head is None:
        return None
    
    # Initialize odd and even pointers
    odd = head             # Start odd pointer at the head
    even = head.next       # Start even pointer at the second node
    even_head = even       # Store the head of the even index nodes
    
    # Traverse the list to reorder nodes
    while even is not None AND even.next is not None:
        odd.next = even.next       # Link current odd node to the next odd-indexed node
        odd = odd.next             # Move the odd pointer forward
        even.next = odd.next       # Link current even node to the next even-indexed node
        even = even.next           # Move the even pointer forward
    
    # Connect the end of the odd list to the head of the even list
    odd.next = even_head
    
    # Return the head of the reordered list
    return head

                                        
The pseudocode provided carefully walks through the logic needed to perform the reordering, ensuring O(1) space complexity by adjusting pointers in-place and achieving O(n) time complexity by traversing the list only once.