Segregate the given Linked List in DAA

Segregate Even and Odd Nodes in a Linked List

A linked list is a linear data structure in which each node has two blocks. One contains the node’s value or data, and the other includes the address of the next field.

Let us assume that we have given a linked list such that each node contains the data and a pointer that 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.

For Example:

Input :

 1-> 2-> 4-> 7-> 9-> 11-> NULL

Output:

1-> 4-> 9 -> 2-> 7-> 11 -> NULL

Explanation: The given linked list after separating each odd index nodes and even indexed terms the linked list will be,

                      1-> 4-> 9 -> 2-> 7-> 11 ->NULL

Approach to solve this Problem:

We will introduce three-pointers for the odd index, even index, and even index value to segregate the given linked list. After that, we will iterate over the whole linked list and initialize the pointer with some value. The indexing starts from ‘1’; thus, the plan’s first node will always be treated as an odd indexed node for any particular string. However, the next of it is treated as an even indexed node.

  • Take Input of 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 output.
  • Initialize three-pointers oddIndex, even index, and evenHead, which are currently pointing to the list’s head.
  • Iterate over the whole linked list and initialize the next pointer of the odd index with the next pointer of even index.
  • Now iterate over the whole list and initialize the next pointer of even index with the odd index’s next pointer.
  • Return the head pointer.

C++ code:

#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 following output:

Output:  

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

Related Topics

DAA: Euclid Algorithm

Euclid Algorithm The Euclid algorithm finds the GCD of two numbers in the efficient time complexity. To find the GCD of two numbers, we take the two numbers’ common factors and multiply...

8 minutes read.

DAA: Algorithm to Find the Maximum Width of a Tree

Algorithm to Find the Maximum Width of a Tree The width of a binary tree is defined as the maximum number of nodes at a given level. The level having the...

5 minutes read.

DAA: KMP Algorithm

KMP ALGORITHM The KMP algorithm is abbreviated as the "Knuth Morris Pratt” algorithm. This algorithm was developed by all of them.  This algorithm searches a pattern of length m in a string...

10 minutes read.

DAA: Floyd Cycle Detection

Floyd Cycle Detection Floyd Cycle algorithm is one of the cycle detection algorithms to detect the cycle in a given singly linked list. In the Floyd Cycle algorithm, we have two pointers...

4 minutes read.

DAA: Insertion Sort Algorithm on Singly Link List

Insertion Sort Algorithm on Singly Link List We will sort a singly link list using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The insertion sort technique works...

3 minutes read.

DAA: Algorithm of Right View of a Binary Tree

Algorithm of Right View of a Binary Tree The right view of a binary tree is the visible nodes from the right side of the tree. In the given tree, the visible...

5 minutes read.

DAA: Find the Height or Maximum Depth of a Binary Tree

Find the Height or Maximum Depth of a Binary Tree We have a binary tree structure and we need to find its height. It is defined by the distance from the...

3 minutes read.

Recurrence relation in DAA

Recurrence relation in DAA The model that uses mathematical concepts to calculate the time complexity of an algorithm is known as the recurrence relational model. A recursive relation, T(n), is a recursive...

5 minutes read.

DAA: Bubble Sort Algorithm

Bubble Sort Algorithm The bubble sort algorithm is also known as the sinking algorithm. In this algorithm, we iterate over the array, and it takes two adjacent elements and swaps them...

3 minutes read.

DAA: Application of DFS and BFS

Application of DFS and BFS Depth-first search and breadth-first searches are the most famous algorithms used in daily life and the programming world. Let us now explore each application in which...

3 minutes read.

DAA: Breadth First Search (BFS) for a Graph

Breadth First Search (Bfs) For A Graph The algorithm in which all the graph nodes are traversed is known as the breadth-first search algorithm. In this algorithm, we select one node,...

5 minutes read.

DAA: Depth-First Search Algorithm

Depth-first search: DFS is a traversing algorithm of a graph or tree in which one node is taken as arbitrary, and with the help of that arbitrary node, all its...

6 minutes read.

DAA: Dijkstra’s Algorithm (Shortest Path)

Dijkstra’s Algorithm (Shortest Path) Dijkstra’s algorithm finds the shortest distance from a source to all the vertices in a graph. This algorithm is used in network protocols like IS-IS and OSPF(Open...

3 minutes read.

DAA: Continuous Tree

Continuous Tree A continuous tree is the one in which the nodes from root to leaf path, the two adjacent node values, have a difference of 1. Input :          3                     /   \                   ...

5 minutes read.

DAA: Rabin Karp Algorithm

Rabin Karp Algorithm The Rabin Karp or Karp Rabin algorithm is used to matching a specific pattern in the string. It uses the technique of hashing to match a specific text. There also...

6 minutes read.

DAA: Binary Tree and its Categories

Binary Tree and its Categories The binary tree is a non-linear data structure in which there are 0 or utmost 2 nodes.  Each node has two children, i.e., left and right...

4 minutes read.

Symmetric Trees in DAA

Symmetric Trees The trees that are mirror images of themselves are known as symmetric trees. Look at the following tree image below: The tree is symmetric as the left subtree is the mirror...

4 minutes read.

DAA: Density of a Binary Tree Algorithm

The Density of a Binary Tree Algorithm The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.  The height of the tree is...

2 minutes read.

DAA: Bubble Sort Algorithm on Linked List

Bubble Sort Algorithm on Linked List In this article, we will sort a Link List using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The bubble sort technique...

4 minutes read.

DAA: Construct a Tree from Inorder and Preorder Traversals

Construct a Tree from Inorder and Preorder Traversals We are given inorder and preorder traversals of a tree. We need to generate a tree from these traversals. Example: Inorder[]   = { 3, 1,...

4 minutes read.