×

Segregate Even and Odd nodes in a Linked List

Segregate even and odd nodes in a Linked List

In this problem, we have given a linked list with integer numbers. We need to modify the given linked list in such a way that all even numbers appear before all the odd numbers in the new linked list. We will also preserve the order of even and odd numbers.

Examples:

Input: 19 -> 13 -> 6 -> 10 -> 2 -> 7 -> 12 -> 1 -> 8 ->NULL

Output: 6 -> 10 -> 2 -> 12 -> 8 -> 19 -> 13 -> 7 -> 1 -> NULL

Note: If all numbers are even then do not change the list

Input: 2 -> 4 -> 6 ->NULL

Output: 2 -> 4 -> 6 -> NULL

Note:If all numbers are odd, then do not change the list

Input: 1 ->3 ->5 ->7 ->NULL

Output: 1 ->3 ->5 ->7 ->NULL

Method 1:

1) Firstly, we will move the pointer to the last node.

2) Then, we will move all the odd nodes to the end.

a) We will consider all odd nodes before the first even node and move them to the end.

b)Next, we will Change the head pointer to point to the first even node.

c) Then, we will consider all odd nodes after the first even node and move them to the end.

C program to segregate even and odd nodes in a Linked List by method 1

 #include<stdio.h>
 #include<stdlib.h>
 //The structure of the node
 struct node
 {
 int info;
 struct node * next;
 };
 struct node * start = NULL;
 // For inserting the elements in the linked list
 void add(int item)
 {
 struct node * t, * p;
 t = (struct node * )malloc( sizeof( struct node ));
 if(start == NULL)
 {
 start = t;
 start -> info = item;
 start -> next = NULL;
 return;
 }
 else
 {
 struct node * p = start;
 while(p -> next != NULL)
 {
 p = p -> next;
 }
 p -> next = t;
 p = p -> next;
 p -> info = item;
 p -> next = NULL;
 }
 }
 // program to segregate even and odd nodes in a Linked List
 voidsegregateEvenOdd(struct node **head_ref)
 {
 struct node *end = *head_ref;
 struct node *prev = NULL;
 struct node *curr = *head_ref;
 while (end->next != NULL)
 end = end->next;
 struct node *new_end = end;
 while (curr->info %2 != 0 &&curr != end)
     {
 new_end->next = curr;
 curr = curr->next;
 new_end->next->next = NULL;
 new_end = new_end->next;
     }
 if (curr->info%2 == 0)
     {
         *head_ref = curr;
 while (curr != end)
         {
 if ( (curr->info)%2 == 0 )
             {
 prev = curr;
 curr = curr->next;
             }
 else
             {
 prev->next = curr->next;
 curr->next = NULL;
 new_end->next = curr;
 new_end = curr;
 curr = prev->next;
             }
         }
     }
 elseprev = curr;
 if (new_end!=end && (end->info)%2 != 0)
     {
 prev->next = end->next;
 end->next = NULL;
 new_end->next = end;
     }
 return;
 }
 // To display the elements of the linked list
 void traverse(struct node * t)
 {
 if(t == NULL)
 {
             printf(" Linked list is empty\n");
                                     }
                                     while(t -> next != NULL)
                                     {
                         printf("%d  ->  ", t -> info);
                         t = t -> next;
                         }
                         printf("%d\n", t -> info);
 }
 // Driver Function
 int main()
 {
 add(19);
 add(13);
 add(6);
 add(10);
 add(2);
 add(7);
 add(12);
 add(1);
 add(8);
 printf("Linked List before:\n");
 traverse(start);
 segregateEvenOdd(&start);
 printf("Linked List after:\n");
 traverse(start);
 return 0;
 } 

Output:

Segregate even and odd nodes in a Linked List

Method 2:

This method will split the given linked list into two parts, one part containing all the even nodes and the other part containing all the odd nodes. Finally, we will link the odd node linked list after the even node linked list.

C program to segregate even and odd nodes in a Linked List by method 2

 #include<stdio.h>
 #include<stdlib.h>
 // The structure of the node
 struct node
 {
 int info;
 struct node * next;
 };
 struct node * start = NULL;
 // For inserting the elements in the linked list
 void add(int item)
 {
 struct node * t, * p;
 t = (struct node * )malloc( sizeof( struct node ));
 if(start == NULL)
 {
 start = t;
 start -> info = item;
 start -> next = NULL;
 return;
 }
 else
 {
 struct node * p = start;
 while(p -> next != NULL)
 {
 p = p -> next;
 }
 p -> next = t;
 p = p -> next;
 p -> info = item;
 p -> next = NULL;
 }
 }
 // program to segregate even and odd nodes in a Linked List
 voidsegregateEvenOdd(struct node **head_ref)
 {
 struct node *evenStart = NULL;
 struct node *evenEnd = NULL;
 struct node *oddStart = NULL;
 struct node *oddEnd = NULL;
 struct node *currNode = *head_ref;
 while(currNode != NULL){
 intval = currNode -> info;
 if(val % 2 == 0) {
 if(evenStart == NULL){
 evenStart = currNode;
 evenEnd = evenStart;
             }
 else{
 evenEnd -> next = currNode;
 evenEnd = evenEnd -> next;
             }
         }
 else{
 if(oddStart == NULL){
 oddStart = currNode;
 oddEnd = oddStart;
             }
 else{
 oddEnd -> next = currNode;
 oddEnd = oddEnd -> next;
             }
         }
 currNode = currNode -> next;
     }
 if(oddStart == NULL || evenStart == NULL){
 return;
     }
 evenEnd -> next = oddStart;
 oddEnd -> next = NULL;
     *head_ref = evenStart;
 }
 // To display the elements of the linked list
 void traverse(struct node * t)
 {
 if(t == NULL)
 {
             printf(" Linked list is empty\n");
                                     }
                                     while(t -> next != NULL)
                                     {
                         printf("%d  ->  ", t -> info);
                         t = t -> next;
                         }
                         printf("%d\n", t -> info);
 }
 // Driver Function
 int main()
 {
 add(3);
 add(5);
 add(9);
 add(11);
 add(13);
 add(15);
 add(17);
 printf("Linked List before:\n");
 traverse(start);
 segregateEvenOdd(&start);
 printf("Linked List after:\n");
 traverse(start);
 return 0;
 } 

Output:

Segregate even and odd nodes in a Linked List

Related Topics

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.

Delete the Middle element of the Linked List in C

Delete the Middle element of the Linked List in C This article has given a singly linked list and will delete the middle element of the given linked list. Example:  The given...

3 minutes read.

Data Structure Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

What is a Threaded Binary Tree?

When we consider those binary trees that are interlinked with each other, we do come across the fact that the fields present in there do consist of NULL values that...

3 minutes read.

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

3 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Threaded Binary Trees

Introduction Threaded Binary Trees (TBTs) are an enhancement of normal binary trees intended for in-order traversal only. This means that this data structure is developed with the objective of making the...

12 minutes read.

Deque in Data Structure

Deque A deque referred as “Double-Ended Queue”, is a linear collection of data items same like queue data structure. deque has two ends, front end and rear end, deque is the...

27 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Boruvkas algorithm

This algorithm is used for finding minimum spanning tree from a weighted graph. Like prim’s and kruskal’s algorithm it is also a greedy algorithm. Note:What is the minimum spanning tree?We know...

4 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

6 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.

What is an AVL Tree in Data Structure?

AVL tree stands for (Adelson, Velskii, & Landis Tree) Data structure Data management is called database management. A data model is a system used to store, manage, and optimize computer resources. Data...

4 minutes read.

What is a Tree in Terms of a Graph?

To know the explanation of trees in terms of graphs, we need first to know what trees and graphs are. So let us first learn about trees and graphs. Trees and...

6 minutes read.

Does Overloading Work with Inheritance

This is a question that occasionally comes to many programmers. Who are curious to know more now has a complete explanation and a solution through this tutorial! Inheritance: The functions of...

3 minutes read.

Binary Tree Inorder Traversal

The binary tree is a type of tree in which each and every node has atleast two children except the leaf nodes. We have various operations in the binary tree,...

4 minutes read.

Queue operations in Data Structure

Queue - Queue is a linear data structure or first in first out data structure means the first element added in the queue will be removed first and the last...

7 minutes read.

Threaded Binary Tree

The linked form of binary trees wastes storage capacity because more than half of the connection variables have a Missing value. A binary tree has several nodes. Hence n+1 link fields...

8 minutes read.

Properties of Binary Tree

Trees are maybe of the most significant datum structures. They are used to store and figure out data. A binarytree is a tree data structure made from nodes, all of which has...

3 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.