# Segregate even and odd nodes in a Linked List

### Problem Statement

In this problem, we are given a LinkedList with even and odd valued nodes and are asked to segregate these nodes. Note that we also have to maintain the order in which the nodes were in the original linked list.  ### Problem Statement Understanding

Let's try to understand the problem with help of an example:

By the way the statement Segregate even and odd nodes indicates that we need to rearrange the nodes of the linked list in such a way that all the even valued nodes appears before odd valued nodes in the modified linked list, and we also have to maintain the order in which the nodes were in the original linked list.

If our linked list is 1→2→3.

Then, while segregating the even and odd nodes of the linked list:

• The node 2 is even valued, so it will come in front, maintaining its order in the original linked list, forming 2.
• The nodes 1 and 3 are odd valued, so will come after 2, maintaining their order in the original linked list, forming 1→3.

Finally, our resultant segregated linked list will be 2→1→3.

If our linked list is 2→1→6→4→8.

Then, while segregating the even and odd nodes of the linked list:

• The nodes 2, 6, 4 and 8 are even valued, so they will come in front, maintaining their order in the original linked list, forming 2→6→4→8.
• The nodes 1 is odd valued, so it will come after 2, 6, 4 and 8 maintaining its order in the original linked list, forming 1.

Finally, our resultant segregated linked list will be 2→6→4→8→1.

Now, I think from the above two examples that it is clear what we have to do in the problems.

Now the main question is how to approach this problem? Try to come up with some approach, not necessarily the optimized one but any approach (if It's brute force it's okay, no problem we will optimize it together).

Well, the most naive idea is to create a new LinkedList by storing the values of the original LinkedList and then inserting the nodes with even value first and then the nodes with odd values, but this will take extra space.

In the above approach we can see that we were able to solve the problem in O(n) time but space complexity is also O(n), so now we need to avoid this extra space.

Note: Generally whenever you get a question on an arrangement in a LinkedList avoid dealing with data rather than try to think of a solution using the manipulation of links.

Let’s try to think of some better approach.

### Approach

The idea is to split the linked list into two parts containing only even nodes and the other containing only odd nodes. The only task then we will perform is to join these two linked lists together. Let’s see this in algorithm form.

### Algorithm

• For every odd valued node, remove that node from the original linked list and place it in another linked list.
• Continue this till the end of the linked list, and then we will be left with our original linked list containing only the even nodes and another linked list with odd nodes.

### Dry Run ### Code Implementation

```

#include
struct Node
{
int data;
struct Node *next;
};
{
Node *evenStart = NULL;
Node *evenEnd = NULL;
Node *oddStart = NULL;
Node *oddEnd = NULL;

while(currNode != NULL){
int val = currNode -> data;
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;
}
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node();
new_node->data = new_data;
}
void printList(struct Node *node)
{
while (node!=NULL)
{
cout<data;
node = node->next;
}
}
int main()
{