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


A single linked list is a linear data structure that contains two parts − one data and other pointers to the next element in the list.

An alternate odd and even singly linked list is a linked list that has one node with even data and the other node with odd data members.

In this problem, we have to rearrange the elements of a predefined singly linked list in either of the two ways which define the alternate odd and even singly linked list.

Two ways are − the first element of the Linked list is even then the next element should be odd and the next to next i.e. third element is even again. The other type would be if the first element is odd then the next element should be even and the next to next i.e. third element is odd.

Let’s look at an example to understand the concept better.

Suppose the linked list is − 45 > 21 > 2 > 213> 3 > 34 > 78>12.

The resultant linked list would be 45 > 2 >21 >34 > 213 > 78 > 3 >12

Now, since in this linked list we have even and odd element and to rearrange these we will place 2 , 34 , 78 ,12 in the consecutive even positions and 45 , 21, 213, 3 in consecutive odd positions.

Now, as we have understood the problem we will try to find a solution for this. There can be multiple ways of solving this type of problem. A simple method would be using stacks. We will create two stacks, one for even and one for odd values. If we encounter any out of order node i.e. even node in odd position, we will push the address to even stack and similarly for odd stack too. And at the end after traversing we will pop nodes out of the stack.

Based on this logic we will create an algorithm −

Algorithm

Step 1 : Create stacks for holding out of order even and odd node of the linked list.
Step 2 : Traverse the linked list and follow :
   Step 2.1 : if odd node is out of order i.e. at odd position, push it to odd stack.
   Step 2.2 : If even node is out of order i.e. at even position, push it to even stack.
Step 3 : Push elements from the stack in alternate order. When the stack is empty, the result is the required linked list.
Step 4: Print the elements of the linked list.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
struct Node {
   int data;
   struct Node* next;
};
void printList(struct Node* node) ;
Node* newNode(int key){
   Node* temp = new Node;
   temp->data = key;
   temp->next = NULL;
   return temp;
}
Node* insertBeg(Node* head, int val){
   Node* temp = newNode(val);
   temp->next = head;
   head = temp;
   return head;
}
void OddEvenList(Node* head) ;
int main(){
   Node* head = newNode(45);
   head = insertBeg(head, 21);
   head = insertBeg(head, 2);
   head = insertBeg(head, 213);
   head = insertBeg(head, 3);
   head = insertBeg(head, 34);
   head = insertBeg(head, 78);
   head = insertBeg(head, 12);
   cout << "Linked List:" << endl;
   printList(head);
   OddEvenList(head);
   cout << "Linked List after "
   << "Rearranging:" << endl;
   printList(head);
   return 0;
}
void printList(struct Node* node){
   while (node != NULL) {
      cout << node->data << " ";
      node = node->next;
   }
   cout << endl;
}
void OddEvenList(Node* head){
   stack<Node*> odd;
   stack<Node*> even;
   int i = 1;
   while (head != nullptr) {
      if (head->data % 2 != 0 && i % 2 == 0) {
         odd.push(head);
      }
      else if (head->data % 2 == 0 && i % 2 != 0) {
         even.push(head);
      }
      head = head->next;
      i++;
   }
   while (!odd.empty() && !even.empty()) {
      swap(odd.top()->data, even.top()->data);
      odd.pop();
      even.pop();
   }
}

Output

Linked List:
12 78 34 3 213 2 21 45
Linked List after Rearranging:
3 78 45 12 213 2 21 34

Updated on: 16-Oct-2019

246 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements