Alternate Odd and Even Nodes in a Singly Linked List in C++



Given a singly linked list, we need to rearrange its nodes so that even and odd numbers come one after the other alternatively. If the list starts with an even number, the next should be odd, then even, and so on. Similarly, if it begins with an odd number, the next should be even, then odd, and so on.

Let's look at some example scenarios to understand the concept better.

Scenario 1

Input: 45 -> 21 -> 2 -> 213 -> 3 -> 34 -> 78 -> 12
Output: 45 -> 2 -> 21 -> 34 -> 213 -> 78 -> 3 -> 12
Explanation:
We start with an odd number, so we arrange the list in odd-even-odd-even order.
We place 45, 21, 213, 3 in the odd positions 
and 2, 34, 78, 12 in the even positions (value-wise, not index-wise).

Scenario 2

Input: 12 -> 4 -> 6 -> 2 -> 7 -> 9 -> 3 -> 11
Output: 12 -> 7 -> 4 -> 9 -> 6 -> 3 -> 2 -> 11
Explanation:
We start with an even number, so we arrange the list in even-odd-even-odd order.
We place 12, 4, 6, 2 in the even value positions 
and 7, 9, 3, 11 in the odd value positions.

Rearranging Nodes in Alternate Even-Odd Order

To alternate odd and even nodes in a singly linked list, we create two separate lists: one for odd nodes and one for even nodes. The idea is to first separate the nodes based on their values and then merge them in an alternate manner.

Algorithm

Following is an algorithm:

  • First, we create two dummy nodes. One will store all the even nodes, and the other will store all the odd nodes.
  • Next, we traverse the original linked list using a pointer. For each node, we check whether its value is even or odd and add it to the respective list.
  • Then, we use two new pointers to go through the even and odd lists, and build the final result by picking one node from each list alternately. Starting with the same type (even or odd) as the first node of the original list.
  • Finally, if one of the lists becomes empty before the other, we attach the remaining nodes from the other list at the end.

C++ Program to Alternate Even and Odd Nodes

Below is a C++ program where we rearrange the linked list so that even and odd nodes appear one after the other, following the same logic explained above.

#include <iostream>
using namespace std;

// Node structure for singly linked list
struct Node {
   int data;
   Node* next;
   Node(int val) : data(val), next(nullptr) {}
};

// Function to print the linked list
void printList(Node* head) {
   while (head) {
      cout << head->data;
      if (head->next) cout << " -> ";
         head = head->next;
   }
   cout << endl;
}

// Function to rearrange nodes to alternate even and odd values
Node* alternateEvenOdd(Node* head) {
   if (!head || !head->next) return head;

   // Dummy nodes for even and odd lists
   Node* evenDummy = new Node(0);
   Node* oddDummy = new Node(0);
   Node* evenTail = evenDummy;
   Node* oddTail = oddDummy;

   // Separate the nodes into even and odd lists
   Node* curr = head;
   while (curr) {
      if (curr->data % 2 == 0) {
         evenTail->next = curr;
         evenTail = evenTail->next;
      } else {
         oddTail->next = curr;
         oddTail = oddTail->next;
      }
      curr = curr->next;
   }

   // End both lists properly
   evenTail->next = nullptr;
   oddTail->next = nullptr;
   
   // Pointers to traverse the even and odd lists
   Node* even = evenDummy->next;
   Node* odd = oddDummy->next;
   
   // Create a new head based on the first node type
   Node* newHead = nullptr;
   Node* tail = nullptr;
   
   // Merge the two lists alternately, starting with the original first node type
   bool startWithEven = (head->data % 2 == 0);
   while (even || odd) {
      if (startWithEven && even) {
         if (!newHead) newHead = even, tail = even;
         else tail->next = even, tail = tail->next;
         even = even->next;
      } else if (!startWithEven && odd) {
         if (!newHead) newHead = odd, tail = odd;
         else tail->next = odd, tail = tail->next;
         odd = odd->next;
      }
       startWithEven = !startWithEven; // Alternate the type
   }
   // Attach remaining nodes if any
   if (even) tail->next = even;
   if (odd) tail->next = odd;
   return newHead;
}

// function to insert node at end
void insert(Node*& head, int val) {
   if (!head) {
      head = new Node(val);
      return;
   }
   Node* temp = head;
   while (temp->next) temp = temp->next;
   temp->next = new Node(val);
}

int main() {
   Node* head = nullptr;
   
   // Creating the linked list
   insert(head, 45);
   insert(head, 21);
   insert(head, 2);
   insert(head, 213);
   insert(head, 3);
   insert(head, 34);
   insert(head, 78);
   insert(head, 12);
   cout << "Original List:\n";
   printList(head);
   // Rearranging to alternate even and odd nodes
   head = alternateEvenOdd(head);
   cout << "Rearranged List:\n";
   printList(head);
   return 0;
}

The output of the above program is shown below, where we rearranged the linked list so that even and odd nodes appear alternately.

Original List: 
45 -> 21 -> 2 -> 213 -> 3 -> 34 -> 78 -> 12

Rearranged List:
45 -> 2 -> 21 -> 34 -> 213 -> 78 -> 3 -> 12

Time Complexity: O(n) because we visit each node once to separate and then merge.

Space Complexity: O(1) because we only use a few pointers, not extra memory.

Conclusion

In this article, we have learnt how to rearrange a singly linked list by separating even and odd nodes and then merging them in alternate order, starting with the first node. This approach is simple and does not require any extra space.

Updated on: 2025-07-29T12:57:44+05:30

351 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements