# Segregate Even and Odd Nodes in a Linked List

Segregate Even and Odd Nodes in a Linked List” is one of the favourite technical interview problem based on linked list asked in many technical interviews of product based companies.

Here, we are given a linked list and our task is to segregate even and odd nodes in a linked list. We need to modify the given linked list in such a way that all odd nodes of linked list appear before the even nodes.

Note: The sequence of odd and even number nodes should not be changed.

```For example, we need to segregate even and odd nodes of a following linked list:
LINKED LIST:       12 -> 13 -> 14 -> 15 -> 16 -> 17
After segregating the nodes,
LINKED LIST:       13 -> 15 -> 17 -> 12 -> 14 -> 16```

The steps required to segregate even and odd nodes in a linked list are as follows:

1. If linked list is empty or contains only one element, then return.
2. Initialize two new lists: odd_list and even_list with NULL, which will contain odd nodes and even nodes of a linked list, respectively.
3. Traverse complete linked list and one by one check whether current node value is odd number or even number. If the node value is odd number, then remove it and add it to odd_list. Similarly, if the node value is even number, then remove it and add it to even_list.
4. Now, traverse the odd_list and attach the last node to the head of the even_list.

#### C++ Program to segregate even and odd nodes in a linked list is as follows:

```/* C++ Program to segregate even and odd nodes in a linked list */
#include<bits/stdc++.h>
using namespace std;

/* structure of the node of the linked list */
typedef struct node
{
int data;
struct node *next;
}Node;

/* Function to Insert node at the end of the linked list */
{
/* Creating a new node */
Node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;

/* check whether the linked list is empty or not */

{
return;
}

/* If linked list is not empty, traverse the complete linked list and point the pointer of
last node of the linked list to the new node */
Node *temp = NULL;
while(temp->next != NULL)
temp=temp->next;
temp->next = new_node;
}
/* Function to Segregate Even and Odd Nodes of the Linked List */
{
/* If Linked List is empty or contains only one node, then simply return. */
return;

/* Initialize 4 node pointers pointing to beginning and end of two new linked list:
odd list and even list.
oddStart - points to the start of the odd node list.
oddEnd - points to the end of the even node list.
evenStart - points to the start of the even node list.
evenEnd - points to the end of the even node List
*/
Node *oddStart = NULL;
Node *evenStart = NULL;
Node *oddEnd = NULL;
Node *evenEnd = NULL;

Node *p = NULL;
/*
Traverse the complete linked list and one by one check whether node value is even or node.
If Node value is odd, remove the node from the list and attach it to the odd list. Similarly,
If Node value is even, remove the node from the list and attach it to the even list.
*/
while(temp != NULL)
{
if(temp -> data %2 == 0)
{
p = temp;
temp = temp -> next;
p -> next = NULL;
if(evenStart == NULL)
{
evenStart = p;
evenEnd = evenStart;
}
else
{
evenEnd -> next = p;
evenEnd = evenEnd -> next;
}
}
else
{
p = temp;
temp = temp -> next;
p -> next = NULL;
if(oddStart == NULL)
{
oddStart = p;
oddEnd = oddStart;
}
else
{
oddEnd -> next = p;
oddEnd = oddEnd -> next;
}
}
}
/* If there are no even nodes in the list, then head will be oddstart */
if(evenStart == NULL)
{
}
/* Else if there are no odd nodes in the list, then head will be evenstart */
else if(oddStart == NULL)
{
}
/* If both even and odd node list exists, then attach the last node of the odd list to the
beginning of the even list */
else
{
oddEnd -> next = evenStart;
}

}

/* Function to Print the Linked List */
{
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}

int main()
{
/* Inserting Some Nodes in the Linked List */
cout<<"The Linked List before segregating is:\n";
cout<<"\nThe Linked List after segregating is:\n";
```OUTPUT: