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: 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: 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: Dynamic Programming

Dynamic Programming Introduction The technique of breaking a problem statement into subproblems and using the optimal result of subproblems as an optimal result of the problem statement is known as dynamic programming....

2 minutes read.

Boyer Moore Algorithm

Boyer Moore Algorithm The Boyer Moore algorithm is a searching algorithm in which a string of length n and a pattern of length m is searched. It prints all the occurrences...

11 minutes read.

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,...

3 minutes read.

DAA: Bottom view of a Binary Tree

Bottom view of a Binary Tree The bottom view of a binary tree is the number of nodes visible when viewed from the bottom. At every horizontal distance, there would be...

3 minutes read.

DAA: Bead Sort Algorithm

Bead Sort Algorithm The bead sort is also known as the gravity sort algorithm. The algorithm is based on the natural phenomena of gravity. The phenomenon is the falling of things...

3 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: 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.

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: 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: 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.

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: Expression Trees

Expression Trees Expression trees are those in which the leaf nodes have the values to be operated, and internal nodes contain the operator on which the leaf node will be performed. Example:...

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.

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: 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: Interpolation Search Algorithm

Interpolation Search Algorithm There is no doubt that binary search is a great algorithm with average time complexity of log n. The feature of discarding one half of the array reduces...

4 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.