How to Segregate a given Linked List in C++

C++Server Side ProgrammingProgramming

A Linked List is a linear data structure in which each node has two blocks such that one block contains the value or data of the node and the other block contains the address of the next field.

Let us assume that we have a linked list such that each node contains the data and a pointer which is pointing to the next node of the linked list. The task is to segregate the given Linked List. Segregating the linked list means we have to separate the odd indexed nodes and even index nodes in the list.

Approach to Solve this Problem

To segregate a given linked list, we will introduce three pointers for the odd index, the even index, and the value at the even index, respectively. After that, we will iterate over the whole linked list and initialize the pointer with some value.

In the linked list, the indexing starts from '1', thus for any particular string, the first node of the list will always be treated as an odd indexed node. However, the next node is treated as an even indexed node.

  • Take a linked list with data and pointer to the next node.
  • A function segregateList(listnode *head) takes the pointer to the head node and returns the segregated linked list as the output.
  • Initialize three pointers oddIndex, evenIndex and evenHead which are currently pointing to the head of the list.
  • Iterate over the whole linked list and initialize the next pointer of oddIndex with the next pointer of evenIndex.
  • Now iterate over the whole list and initialize the next pointer of evenIndex with the next pointer of oddIndex.
  • Return the head pointer.

Example

Live Demo

#include <iostream>
using namespace std;
class node {
   public:
      int data;
   node * next;
   node(int d) {
      data = d;
      next = NULL;
   }
};
node * segregateList(node * head) {
   if (head == NULL) {
      return NULL;
   }
   node * oddIndex = head;
   node * evenIndex = head -> next;
   node * evenHead = evenIndex;
   while (evenIndex != NULL and evenIndex -> next != NULL) {
      oddIndex -> next = evenIndex -> next;
      oddIndex = oddIndex -> next;
      evenIndex -> next = oddIndex -> next;
      evenIndex = evenIndex -> next;
   }
   oddIndex -> next = evenHead;
   return head;
}
void insertAtNode(node * & head, int data) {
   node * n = new node(data);
   n -> next = head;
   head = n;
}
void print(node * head) {
   while (head != NULL) {
      cout << head -> data << "->";
      head = head -> next;
   }
}
int main() {
   node * head = NULL;
   // It could be possible that the head node contains NULL Value.
   insertAtNode(head, 5);
   insertAtNode(head, 8);
   insertAtNode(head, 3);
   insertAtNode(head, 1);
   insertAtNode(head, 2);
   print(head);
   cout << endl;
   segregateList(head);
   print(head);
}

Running the above code will generate the output as,

Output

2->3->5->1->8->

The given linked list is, 2->1->3->8->5->. After segregating the linked list, it will produce the output as, 2->3->5->1->8->.

raja
Published on 23-Feb-2021 05:17:00
Advertisements