# Arrange even and odd elements of a linked list

Given collection of integer element in a linked list, Our goal is to arrange the linked list node Even after Odd elements.

```
Example 1
Linked List    :    6  4  5  10  3  7  9  2  8
After arrange  :
Linked List    :    6  5  4  3  10  7  2  9  8
[Even node -> Odd node -> Even node -> Odd node etc]

Example 2
Linked List    :    1  2  3  4  5  6
After arrange  :
Linked List    :    2  1  4  3  6  5```

In above example linked list contains same length of even and odd nodes. When Even and Odd nodes length are not same so extra nodes are added at end of resultant linked list. For example.

```
// When Odd nodes are more than Even
Example 1
Linked List :   1  1  4  2  3  1

{
Even Nodes :  4  2
Odd  Nodes :  1  1  3  1

Combine Element
[4  1]

//Remaining
Even Nodes :  2
Odd  Nodes :  1  3  1

Combine Element
[4  1  2  1]

//Remaining
Even Nodes :
Odd  Nodes :  3  1

Final Element
[4  1  2  1  3  1]
}

After arrange :
Linked List :   4  1  2  1  3  1

// When Even nodes are more than Odd
Example 2
Linked List :   2  6  4  8  3  3  2

{
Even Nodes :  2  6  4  8  2
Odd  Nodes :  3  3

Combine Element
[3  2]

//Remaining
Even Nodes :  6  4  8  2
Odd  Nodes :  3

Combine Element
[3  2  6  3]

//Remaining
Even Nodes :  4  8  2
Odd  Nodes :

Final Element
[3  2  6  3  4  8  2]

}
After arrange :
Linked List :   2  3  6  3  4  8  2```

Here given code implementation process.

``````// C Program
// Arrange even and odd elements of a linked list

#include <stdio.h>
//For malloc function
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
void insert(struct Node **head, int data)
{
struct Node *node = create_node(data);
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
{
{
return;
}
while (temp != NULL)
{
printf("  %d", temp->data);
//visit to next node
temp = temp->next;
}
}
//Arrange alternative even and odd nodes in linked list
{
//Define some auxiliary variables
struct Node *even_node = NULL;
struct Node *odd_node = NULL;
struct Node *tail_1 = NULL;
struct Node *tail_2 = NULL;
struct Node *temp = NULL;
//Segregation of even and odd nodes elements
while (auxiliary != NULL)
{
if (auxiliary->data % 2 == 0)
{
//Even Node element
if (even_node == NULL)
{
even_node = auxiliary;
}
else
{
//Add node at end of even list
tail_1->next = auxiliary;
}
//Get new last node
tail_1 = auxiliary;
//visit to next node
auxiliary = auxiliary->next;
//set next node is null
tail_1->next = NULL;
}
else
{
//Odd Node element
if (odd_node == NULL)
{
odd_node = auxiliary;
}
else
{
// Add node at end of odd list
tail_2->next = auxiliary;
}
//Get new last node
tail_2 = auxiliary;
//visit to next node
auxiliary = auxiliary->next;
//set next node is null
tail_2->next = NULL;
}
}
if (even_node != NULL)
{
// new first node of linked list
}
else
{
//When only odd element exists in linked list
}
//Combine even and odd nodes
while (even_node != NULL && odd_node != NULL)
{
//next node of even list
tail_1 = even_node->next;
//next node of odd list
tail_2 = odd_node->next;
even_node->next = odd_node;
if (tail_1 != NULL)
{
// This is useful to handle
// when even linked list next node is empty
// and odd elements are next node not empty
odd_node->next = tail_1;
}
//Visit to next node
even_node = tail_1;
odd_node = tail_2;
}
}
int main()
{
struct Node *list1 = NULL;
struct Node *list2 = NULL;
struct Node *list3 = NULL;
insert( &list1, 6);
insert( &list1, 4);
insert( &list1, 5);
insert( &list1, 10);
insert( &list1, 3);
insert( &list1, 7);
insert( &list1, 9);
insert( &list1, 2);
insert( &list1, 8);
printf(" Before arrange : ");
//Before arrange nodes
display(list1);
//When linked list contains same length of Even and Odd nodes
list1 = arrange_even_odds(list1);
printf("\n After arrange  : ");
//After arrange nodes
display(list1);
insert( &list2, 1);
insert( &list2, 1);
insert( &list2, 4);
insert( &list2, 2);
insert( &list2, 3);
insert( &list2, 1);
printf("\n\n Before arrange : ");
//Before arrange nodes
display(list2);
//When linked list Odd nodes are more than Even
list2 = arrange_even_odds(list2);
printf("\n After arrange  : ");
//After arrange nodes
display(list2);
insert( &list3, 2);
insert( &list3, 6);
insert( &list3, 4);
insert( &list3, 8);
insert( &list3, 3);
insert( &list3, 3);
insert( &list3, 2);
printf("\n\n Before arrange : ");
//Before arrange nodes
display(list3);
//When linked list Odd nodes are more than Even
list3 = arrange_even_odds(list3);
printf("\n After arrange  : ");
//After arrange nodes
display(list3);
return 0;
}``````

#### Output

`````` Before arrange :   6  4  5  10  3  7  9  2  8
After arrange  :   6  5  4  3  10  7  2  9  8

Before arrange :   1  1  4  2  3  1
After arrange  :   4  1  2  1  3  1

Before arrange :   2  6  4  8  3  3  2
After arrange  :   2  3  6  3  4  8  2``````
``````// Java Program
// Arrange even and odd elements of a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
System.out.print("  " + temp.data);
//visit to next node
temp = temp.next;
}
}
//Arrange alternative even and odd nodes in linked list
public void arrange_even_odds()
{
//Define some auxiliary variables
Node even_node = null;
Node odd_node = null;
Node tail_1 = null;
Node tail_2 = null;
Node temp = null;
//Segregation of even and odd nodes elements
while (auxiliary != null)
{
if (auxiliary.data % 2 == 0)
{
//Even Node element
if (even_node == null)
{
even_node = auxiliary;
}
else
{
//Add node at end of even list
tail_1.next = auxiliary;
}
//Get new last node
tail_1 = auxiliary;
//visit to next node
auxiliary = auxiliary.next;
//set next node is null
tail_1.next = null;
}
else
{
//Odd Node element
if (odd_node == null)
{
odd_node = auxiliary;
}
else
{
// Add node at end of odd list
tail_2.next = auxiliary;
}
//Get new last node
tail_2 = auxiliary;
//visit to next node
auxiliary = auxiliary.next;
//set next node is null
tail_2.next = null;
}
}
if (even_node != null)
{
// new first node of linked list
}
else
{
//When only odd element exists in linked list
}
//Combine even and odd nodes
while (even_node != null && odd_node != null)
{
//next node of even list
tail_1 = even_node.next;
//next node of odd list
tail_2 = odd_node.next;
even_node.next = odd_node;
if (tail_1 != null)
{
// This is useful to handle
// when even linked list next node is empty
// and odd elements are next node not empty
odd_node.next = tail_1;
}
//Visit to next node
even_node = tail_1;
odd_node = tail_2;
}
}
public static void main(String[] args)
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
System.out.print(" Before arrange : ");
//Before arrange nodes
list1.display();
//When linked list contains same length of Even and Odd nodes
list1.arrange_even_odds();
System.out.print("\n After arrange  : ");
//After arrange nodes
list1.display();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
System.out.print("\n\n Before arrange : ");
//Before arrange nodes
list2.display();
//When linked list Odd nodes are more than Even
list2.arrange_even_odds();
System.out.print("\n After arrange  : ");
//After arrange nodes
list2.display();
list3.insert(2);
list3.insert(6);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
System.out.print("\n\n Before arrange : ");
//Before arrange nodes
list3.display();
//When linked list Odd nodes are more than Even
list3.arrange_even_odds();
System.out.print("\n After arrange  : ");
//After arrange nodes
list3.display();
}
}``````

#### Output

`````` Before arrange :
Linked List    :    6  4  5  10  3  7  9  2  8
After arrange  :
Linked List    :    6  5  4  3  10  7  2  9  8

Before arrange :
Linked List    :    1  1  4  2  3  1
After arrange  :
Linked List    :    4  1  2  1  3  1

Before arrange :
Linked List    :    2  6  4  8  3  3  2
After arrange  :
Linked List    :    2  3  6  3  4  8  2``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Arrange even and odd elements of a linked list

class Node
{
public:
int data;
Node *next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
public:
Node *tail;
//Class constructors
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node *node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
cout << "\n Linked List    :  ";
while (temp != NULL)
{
//display node value
cout << "  " << temp->data;
//visit to next node
temp = temp->next;
}
}
//Arrange alternative even and odd nodes in linked list
void arrange_even_odds()
{
//Define some auxiliary variables
Node *even_node = NULL;
Node *odd_node = NULL;
Node *tail_1 = NULL;
Node *tail_2 = NULL;
Node *temp = NULL;
//Segregation of even and odd nodes elements
while (auxiliary != NULL)
{
if (auxiliary->data % 2 == 0)
{
//Even Node element
if (even_node == NULL)
{
even_node = auxiliary;
}
else
{
//Add node at end of even list
tail_1->next = auxiliary;
}
//Get new last node
tail_1 = auxiliary;
//visit to next node
auxiliary = auxiliary->next;
//set next node is null
tail_1->next = NULL;
}
else
{
//Odd Node element
if (odd_node == NULL)
{
odd_node = auxiliary;
}
else
{
// Add node at end of odd list
tail_2->next = auxiliary;
}
//Get new last node
tail_2 = auxiliary;
//visit to next node
auxiliary = auxiliary->next;
//set next node is null
tail_2->next = NULL;
}
}
if (even_node != NULL)
{
// new first node of linked list
}
else
{
//When only odd element exists in linked list
}
//Combine even and odd nodes
while (even_node != NULL && odd_node != NULL)
{
//next node of even list
tail_1 = even_node->next;
//next node of odd list
tail_2 = odd_node->next;
even_node->next = odd_node;
if (tail_1 != NULL)
{
// This is useful to handle
// when even linked list next node is empty
// and odd elements are next node not empty
odd_node->next = tail_1;
}
//Visit to next node
even_node = tail_1;
odd_node = tail_2;
}
}
};
int main()
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
cout << " Before arrange : ";
//Before arrange nodes
list1.display();
//When linked list contains same length of Even and Odd nodes
list1.arrange_even_odds();
cout << "\n After arrange  : ";
//After arrange nodes
list1.display();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
cout << "\n\n Before arrange : ";
//Before arrange nodes
list2.display();
//When linked list Odd nodes are more than Even
list2.arrange_even_odds();
cout << "\n After arrange  : ";
//After arrange nodes
list2.display();
list3.insert(2);
list3.insert(6);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
cout << "\n\n Before arrange : ";
//Before arrange nodes
list3.display();
//When linked list Odd nodes are more than Even
list3.arrange_even_odds();
cout << "\n After arrange  : ";
//After arrange nodes
list3.display();
return 0;
}``````

#### Output

`````` Before arrange :
Linked List    :    6  4  5  10  3  7  9  2  8
After arrange  :
Linked List    :    6  5  4  3  10  7  2  9  8

Before arrange :
Linked List    :    1  1  4  2  3  1
After arrange  :
Linked List    :    4  1  2  1  3  1

Before arrange :
Linked List    :    2  6  4  8  3  3  2
After arrange  :
Linked List    :    2  3  6  3  4  8  2``````
``````//Include namespace system
using System;
// C# Program
// Arrange even and odd elements of a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
Console.Write("  " + temp.data);
//visit to next node
temp = temp.next;
}
}
//Arrange alternative even and odd nodes in linked list
public void arrange_even_odds()
{
//Define some auxiliary variables
Node even_node = null;
Node odd_node = null;
Node tail_1 = null;
Node tail_2 = null;

//Segregation of even and odd nodes elements
while (auxiliary != null)
{
if (auxiliary.data % 2 == 0)
{
//Even Node element
if (even_node == null)
{
even_node = auxiliary;
}
else
{
//Add node at end of even list
tail_1.next = auxiliary;
}
//Get new last node
tail_1 = auxiliary;
//visit to next node
auxiliary = auxiliary.next;
//set next node is null
tail_1.next = null;
}
else
{
//Odd Node element
if (odd_node == null)
{
odd_node = auxiliary;
}
else
{
// Add node at end of odd list
tail_2.next = auxiliary;
}
//Get new last node
tail_2 = auxiliary;
//visit to next node
auxiliary = auxiliary.next;
//set next node is null
tail_2.next = null;
}
}
if (even_node != null)
{
// new first node of linked list
}
else
{
//When only odd element exists in linked list
}
//Combine even and odd nodes
while (even_node != null && odd_node != null)
{
//next node of even list
tail_1 = even_node.next;
//next node of odd list
tail_2 = odd_node.next;
even_node.next = odd_node;
if (tail_1 != null)
{
// This is useful to handle
// when even linked list next node is empty
// and odd elements are next node not empty
odd_node.next = tail_1;
}
//Visit to next node
even_node = tail_1;
odd_node = tail_2;
}
}
public static void Main(String[] args)
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
Console.Write(" Before arrange : ");
//Before arrange nodes
list1.display();
//When linked list contains same length of Even and Odd nodes
list1.arrange_even_odds();
Console.Write("\n After arrange  : ");
//After arrange nodes
list1.display();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
Console.Write("\n\n Before arrange : ");
//Before arrange nodes
list2.display();
//When linked list Odd nodes are more than Even
list2.arrange_even_odds();
Console.Write("\n After arrange  : ");
//After arrange nodes
list2.display();
list3.insert(2);
list3.insert(6);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
Console.Write("\n\n Before arrange : ");
//Before arrange nodes
list3.display();
//When linked list Odd nodes are more than Even
list3.arrange_even_odds();
Console.Write("\n After arrange  : ");
//After arrange nodes
list3.display();
}
}``````

#### Output

`````` Before arrange :
Linked List    :    6  4  5  10  3  7  9  2  8
After arrange  :
Linked List    :    6  5  4  3  10  7  2  9  8

Before arrange :
Linked List    :    1  1  4  2  3  1
After arrange  :
Linked List    :    4  1  2  1  3  1

Before arrange :
Linked List    :    2  6  4  8  3  3  2
After arrange  :
Linked List    :    2  3  6  3  4  8  2``````
``````<?php
// Php Program
// Arrange even and odd elements of a linked list

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructors
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$data)
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
echo "\n Linked List    :  ";
while (\$temp != null)
{
//display node value
echo "  ". \$temp->data;
//visit to next node
\$temp = \$temp->next;
}
}
//Arrange alternative even and odd nodes in linked list
public	function arrange_even_odds()
{
//Define some auxiliary variables
\$even_node = null;
\$odd_node = null;
\$tail_1 = null;
\$tail_2 = null;
//Segregation of even and odd nodes elements
while (\$auxiliary != null)
{
if (\$auxiliary->data % 2 == 0)
{
//Even Node element
if (\$even_node == null)
{
\$even_node = \$auxiliary;
}
else
{
//Add node at end of even list
\$tail_1->next = \$auxiliary;
}
//Get new last node
\$tail_1 = \$auxiliary;
//visit to next node
\$auxiliary = \$auxiliary->next;
//set next node is null
\$tail_1->next = null;
}
else
{
//Odd Node element
if (\$odd_node == null)
{
\$odd_node = \$auxiliary;
}
else
{
// Add node at end of odd list
\$tail_2->next = \$auxiliary;
}
//Get new last node
\$tail_2 = \$auxiliary;
//visit to next node
\$auxiliary = \$auxiliary->next;
//set next node is null
\$tail_2->next = null;
}
}
if (\$even_node != null)
{
// new first node of linked list
}
else
{
//When only odd element exists in linked list
}
//Combine even and odd nodes
while (\$even_node != null && \$odd_node != null)
{
//next node of even list
\$tail_1 = \$even_node->next;
//next node of odd list
\$tail_2 = \$odd_node->next;
\$even_node->next = \$odd_node;
if (\$tail_1 != null)
{
// This is useful to handle
// when even linked list next node is empty
// and odd elements are next node not empty
\$odd_node->next = \$tail_1;
}
//Visit to next node
\$even_node = \$tail_1;
\$odd_node = \$tail_2;
}
}
}

function main()
{
\$list1->insert(6);
\$list1->insert(4);
\$list1->insert(5);
\$list1->insert(10);
\$list1->insert(3);
\$list1->insert(7);
\$list1->insert(9);
\$list1->insert(2);
\$list1->insert(8);
echo " Before arrange : ";
//Before arrange nodes
\$list1->display();
//When linked list contains same length of Even and Odd nodes
\$list1->arrange_even_odds();
echo "\n After arrange  : ";
//After arrange nodes
\$list1->display();
\$list2->insert(1);
\$list2->insert(1);
\$list2->insert(4);
\$list2->insert(2);
\$list2->insert(3);
\$list2->insert(1);
echo "\n\n Before arrange : ";
//Before arrange nodes
\$list2->display();
//When linked list Odd nodes are more than Even
\$list2->arrange_even_odds();
echo "\n After arrange  : ";
//After arrange nodes
\$list2->display();
\$list3->insert(2);
\$list3->insert(6);
\$list3->insert(4);
\$list3->insert(8);
\$list3->insert(3);
\$list3->insert(3);
\$list3->insert(2);
echo "\n\n Before arrange : ";
//Before arrange nodes
\$list3->display();
//When linked list Odd nodes are more than Even
\$list3->arrange_even_odds();
echo "\n After arrange  : ";
//After arrange nodes
\$list3->display();
}
main();``````

#### Output

`````` Before arrange :
Linked List    :    6  4  5  10  3  7  9  2  8
After arrange  :
Linked List    :    6  5  4  3  10  7  2  9  8

Before arrange :
Linked List    :    1  1  4  2  3  1
After arrange  :
Linked List    :    4  1  2  1  3  1

Before arrange :
Linked List    :    2  6  4  8  3  3  2
After arrange  :
Linked List    :    2  3  6  3  4  8  2``````
``````// Node Js Program
// Arrange even and odd elements of a linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructors
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//Create a node
var node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
//display node value
process.stdout.write("  " + temp.data);
//visit to next node
temp = temp.next;
}
}
//Arrange alternative even and odd nodes in linked list
arrange_even_odds()
{
//Define some auxiliary variables
var even_node = null;
var odd_node = null;
var tail_1 = null;
var tail_2 = null;
//Segregation of even and odd nodes elements
while (auxiliary != null)
{
if (auxiliary.data % 2 == 0)
{
//Even Node element
if (even_node == null)
{
even_node = auxiliary;
}
else
{
//Add node at end of even list
tail_1.next = auxiliary;
}
//Get new last node
tail_1 = auxiliary;
//visit to next node
auxiliary = auxiliary.next;
//set next node is null
tail_1.next = null;
}
else
{
//Odd Node element
if (odd_node == null)
{
odd_node = auxiliary;
}
else
{
// Add node at end of odd list
tail_2.next = auxiliary;
}
//Get new last node
tail_2 = auxiliary;
//visit to next node
auxiliary = auxiliary.next;
//set next node is null
tail_2.next = null;
}
}
if (even_node != null)
{
// new first node of linked list
}
else
{
//When only odd element exists in linked list
}
//Combine even and odd nodes
while (even_node != null && odd_node != null)
{
//next node of even list
tail_1 = even_node.next;
//next node of odd list
tail_2 = odd_node.next;
even_node.next = odd_node;
if (tail_1 != null)
{
// This is useful to handle
// when even linked list next node is empty
// and odd elements are next node not empty
odd_node.next = tail_1;
}
//Visit to next node
even_node = tail_1;
odd_node = tail_2;
}
}
}

function main()
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
process.stdout.write(" Before arrange : ");
//Before arrange nodes
list1.display();
//When linked list contains same length of Even and Odd nodes
list1.arrange_even_odds();
process.stdout.write("\n After arrange  : ");
//After arrange nodes
list1.display();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
process.stdout.write("\n\n Before arrange : ");
//Before arrange nodes
list2.display();
//When linked list Odd nodes are more than Even
list2.arrange_even_odds();
process.stdout.write("\n After arrange  : ");
//After arrange nodes
list2.display();
list3.insert(2);
list3.insert(6);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
process.stdout.write("\n\n Before arrange : ");
//Before arrange nodes
list3.display();
//When linked list Odd nodes are more than Even
list3.arrange_even_odds();
process.stdout.write("\n After arrange  : ");
//After arrange nodes
list3.display();
}
main();``````

#### Output

`````` Before arrange :
Linked List    :    6  4  5  10  3  7  9  2  8
After arrange  :
Linked List    :    6  5  4  3  10  7  2  9  8

Before arrange :
Linked List    :    1  1  4  2  3  1
After arrange  :
Linked List    :    4  1  2  1  3  1

Before arrange :
Linked List    :    2  6  4  8  3  3  2
After arrange  :
Linked List    :    2  3  6  3  4  8  2``````
``````#  Python 3 Program
#  Arrange even and odd elements of a linked list

class Node :

def __init__(self, data) :
# Set node value
self.data = data
self.next = None

# Class constructors
def __init__(self) :
self.tail = None

# insert node at last of linke list
def insert(self, data) :
# Create a node
node = Node(data)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print("\nEmpty linked list\n", end = "")
return

print("\n Linked List    :  ", end = "")
while (temp != None) :
# display node value
print("  ", temp.data, end = "")
# visit to next node
temp = temp.next

# Arrange alternative even and odd nodes in linked list
def arrange_even_odds(self) :
# Define some auxiliary variables
even_node = None
odd_node = None
tail_1 = None
tail_2 = None
# Segregation of even and odd nodes elements
while (auxiliary != None) :
if (auxiliary.data % 2 == 0) :
# Even Node element
if (even_node == None) :
even_node = auxiliary
else :
# Add node at end of even list
tail_1.next = auxiliary

# Get new last node
tail_1 = auxiliary
# visit to next node
auxiliary = auxiliary.next
# set next node is null
tail_1.next = None
else :
# Odd Node element
if (odd_node == None) :
odd_node = auxiliary
else :
#  Add node at end of odd list
tail_2.next = auxiliary

# Get new last node
tail_2 = auxiliary
# visit to next node
auxiliary = auxiliary.next
# set next node is null
tail_2.next = None

if (even_node != None) :
#  new first node of linked list
else :
# When only odd element exists in linked list

# Combine even and odd nodes
while (even_node != None and odd_node != None) :
# next node of even list
tail_1 = even_node.next
# next node of odd list
tail_2 = odd_node.next
even_node.next = odd_node
if (tail_1 != None) :
#  This is useful to handle
#  when even linked list next node is empty
#  and odd elements are next node not empty
odd_node.next = tail_1

# Visit to next node
even_node = tail_1
odd_node = tail_2

def main() :
list1.insert(6)
list1.insert(4)
list1.insert(5)
list1.insert(10)
list1.insert(3)
list1.insert(7)
list1.insert(9)
list1.insert(2)
list1.insert(8)
print(" Before arrange : ", end = "")
# Before arrange nodes
list1.display()
# When linked list contains same length of Even and Odd nodes
list1.arrange_even_odds()
print("\n After arrange  : ", end = "")
# After arrange nodes
list1.display()
list2.insert(1)
list2.insert(1)
list2.insert(4)
list2.insert(2)
list2.insert(3)
list2.insert(1)
print("\n\n Before arrange : ", end = "")
# Before arrange nodes
list2.display()
# When linked list Odd nodes are more than Even
list2.arrange_even_odds()
print("\n After arrange  : ", end = "")
# After arrange nodes
list2.display()
list3.insert(2)
list3.insert(6)
list3.insert(4)
list3.insert(8)
list3.insert(3)
list3.insert(3)
list3.insert(2)
print("\n\n Before arrange : ", end = "")
# Before arrange nodes
list3.display()
# When linked list Odd nodes are more than Even
list3.arrange_even_odds()
print("\n After arrange  : ", end = "")
# After arrange nodes
list3.display()

if __name__ == "__main__": main()``````

#### Output

`````` Before arrange :
Linked List    :     6   4   5   10   3   7   9   2   8
After arrange  :
Linked List    :     6   5   4   3   10   7   2   9   8

Before arrange :
Linked List    :     1   1   4   2   3   1
After arrange  :
Linked List    :     4   1   2   1   3   1

Before arrange :
Linked List    :     2   6   4   8   3   3   2
After arrange  :
Linked List    :     2   3   6   3   4   8   2``````
``````#  Ruby Program
#  Arrange even and odd elements of a linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)

# Set node value
self.data = data
self.next = nil
end
end

# Class constructors
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(data)

# Create a node
node = Node.new(data)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

return
end
while (temp != nil)

# display node value
print("  ", temp.data)
# visit to next node
temp = temp.next
end
end
# Arrange alternative even and odd nodes in linked list
def arrange_even_odds()

# Define some auxiliary variables
even_node = nil
odd_node = nil
tail_1 = nil
tail_2 = nil
# Segregation of even and odd nodes elements
while (auxiliary != nil)

if (auxiliary.data % 2 == 0)

# Even Node element
if (even_node == nil)

even_node = auxiliary
else

# Add node at end of even list
tail_1.next = auxiliary
end
# Get new last node
tail_1 = auxiliary
# visit to next node
auxiliary = auxiliary.next
# set next node is null
tail_1.next = nil
else

# Odd Node element
if (odd_node == nil)

odd_node = auxiliary
else

#  Add node at end of odd list
tail_2.next = auxiliary
end
# Get new last node
tail_2 = auxiliary
# visit to next node
auxiliary = auxiliary.next
# set next node is null
tail_2.next = nil
end
end
if (even_node != nil)

#  new first node of linked list
else

# When only odd element exists in linked list
end
# Combine even and odd nodes
while (even_node != nil && odd_node != nil)

# next node of even list
tail_1 = even_node.next
# next node of odd list
tail_2 = odd_node.next
even_node.next = odd_node
if (tail_1 != nil)

#  This is useful to handle
#  when even linked list next node is empty
#  and odd elements are next node not empty
odd_node.next = tail_1
end
# Visit to next node
even_node = tail_1
odd_node = tail_2
end
end
end
def main()

list1.insert(6)
list1.insert(4)
list1.insert(5)
list1.insert(10)
list1.insert(3)
list1.insert(7)
list1.insert(9)
list1.insert(2)
list1.insert(8)
print(" Before arrange : ")
# Before arrange nodes
list1.display()
# When linked list contains same length of Even and Odd nodes
list1.arrange_even_odds()
print("\n After arrange  : ")
# After arrange nodes
list1.display()
list2.insert(1)
list2.insert(1)
list2.insert(4)
list2.insert(2)
list2.insert(3)
list2.insert(1)
print("\n\n Before arrange : ")
# Before arrange nodes
list2.display()
# When linked list Odd nodes are more than Even
list2.arrange_even_odds()
print("\n After arrange  : ")
# After arrange nodes
list2.display()
list3.insert(2)
list3.insert(6)
list3.insert(4)
list3.insert(8)
list3.insert(3)
list3.insert(3)
list3.insert(2)
print("\n\n Before arrange : ")
# Before arrange nodes
list3.display()
# When linked list Odd nodes are more than Even
list3.arrange_even_odds()
print("\n After arrange  : ")
# After arrange nodes
list3.display()
end
main()``````

#### Output

`````` Before arrange :
Linked List    :    6  4  5  10  3  7  9  2  8
After arrange  :
Linked List    :    6  5  4  3  10  7  2  9  8

Before arrange :
Linked List    :    1  1  4  2  3  1
After arrange  :
Linked List    :    4  1  2  1  3  1

Before arrange :
Linked List    :    2  6  4  8  3  3  2
After arrange  :
Linked List    :    2  3  6  3  4  8  2``````
``````// Scala Program
// Arrange even and odd elements of a linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructors
def this()
{
this(null, null);
}
//insert node at last of linke list
def insert(data: Int): Unit = {
//Create a node
var node: Node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
//display node value
print("  " + temp.data);
//visit to next node
temp = temp.next;
}
}
//Arrange alternative even and odd nodes in linked list
def arrange_even_odds(): Unit = {
//Define some auxiliary variables
var even_node: Node = null;
var odd_node: Node = null;
var tail_1: Node = null;
var tail_2: Node = null;
//Segregation of even and odd nodes elements
while (auxiliary != null)
{
if (auxiliary.data % 2 == 0)
{
//Even Node element
if (even_node == null)
{
even_node = auxiliary;
}
else
{
//Add node at end of even list
tail_1.next = auxiliary;
}
//Get new last node
tail_1 = auxiliary;
//visit to next node
auxiliary = auxiliary.next;
//set next node is null
tail_1.next = null;
}
else
{
//Odd Node element
if (odd_node == null)
{
odd_node = auxiliary;
}
else
{
// Add node at end of odd list
tail_2.next = auxiliary;
}
//Get new last node
tail_2 = auxiliary;
//visit to next node
auxiliary = auxiliary.next;
//set next node is null
tail_2.next = null;
}
}
if (even_node != null)
{
// new first node of linked list
}
else
{
//When only odd element exists in linked list
}
//Combine even and odd nodes
while (even_node != null && odd_node != null)
{
//next node of even list
tail_1 = even_node.next;
//next node of odd list
tail_2 = odd_node.next;
even_node.next = odd_node;
if (tail_1 != null)
{
// This is useful to handle
// when even linked list next node is empty
// and odd elements are next node not empty
odd_node.next = tail_1;
}
//Visit to next node
even_node = tail_1;
odd_node = tail_2;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
print(" Before arrange : ");
//Before arrange nodes
list1.display();
//When linked list contains same length of Even and Odd nodes
list1.arrange_even_odds();
print("\n After arrange  : ");
//After arrange nodes
list1.display();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
print("\n\n Before arrange : ");
//Before arrange nodes
list2.display();
//When linked list Odd nodes are more than Even
list2.arrange_even_odds();
print("\n After arrange  : ");
//After arrange nodes
list2.display();
list3.insert(2);
list3.insert(6);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
print("\n\n Before arrange : ");
//Before arrange nodes
list3.display();
//When linked list Odd nodes are more than Even
list3.arrange_even_odds();
print("\n After arrange  : ");
//After arrange nodes
list3.display();
}
}``````

#### Output

`````` Before arrange :
Linked List    :    6  4  5  10  3  7  9  2  8
After arrange  :
Linked List    :    6  5  4  3  10  7  2  9  8

Before arrange :
Linked List    :    1  1  4  2  3  1
After arrange  :
Linked List    :    4  1  2  1  3  1

Before arrange :
Linked List    :    2  6  4  8  3  3  2
After arrange  :
Linked List    :    2  3  6  3  4  8  2``````
``````// Swift Program
// Arrange even and odd elements of a linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructors
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
return;
}
print("\n Linked List    :  ", terminator: "");
while (temp != nil)
{
//display node value
print("  ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
}
//Arrange alternative even and odd nodes in linked list
func arrange_even_odds()
{
//Define some auxiliary variables
var even_node: Node? = nil;
var odd_node: Node? = nil;
var tail_1: Node? = nil;
var tail_2: Node? = nil;
//Segregation of even and odd nodes elements
while (auxiliary != nil)
{
if (auxiliary!.data % 2 == 0)
{
//Even Node element
if (even_node == nil)
{
even_node = auxiliary;
}
else
{
//Add node at end of even list
tail_1!.next = auxiliary;
}
//Get new last node
tail_1 = auxiliary;
//visit to next node
auxiliary = auxiliary!.next;
//set next node is null
tail_1!.next = nil;
}
else
{
//Odd Node element
if (odd_node == nil)
{
odd_node = auxiliary;
}
else
{
// Add node at end of odd list
tail_2!.next = auxiliary;
}
//Get new last node
tail_2 = auxiliary;
//visit to next node
auxiliary = auxiliary!.next;
//set next node is null
tail_2!.next = nil;
}
}
if (even_node != nil)
{
// new first node of linked list
}
else
{
//When only odd element exists in linked list
}
//Combine even and odd nodes
while (even_node != nil && odd_node != nil)
{
//next node of even list
tail_1 = even_node!.next;
//next node of odd list
tail_2 = odd_node!.next;
even_node!.next = odd_node;
if (tail_1 != nil)
{
// This is useful to handle
// when even linked list next node is empty
// and odd elements are next node not empty
odd_node!.next = tail_1;
}
//Visit to next node
even_node = tail_1;
odd_node = tail_2;
}
}
}
func main()
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
print(" Before arrange : ", terminator: "");
//Before arrange nodes
list1.display();
//When linked list contains same length of Even and Odd nodes
list1.arrange_even_odds();
print("\n After arrange  : ", terminator: "");
//After arrange nodes
list1.display();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
print("\n\n Before arrange : ", terminator: "");
//Before arrange nodes
list2.display();
//When linked list Odd nodes are more than Even
list2.arrange_even_odds();
print("\n After arrange  : ", terminator: "");
//After arrange nodes
list2.display();
list3.insert(2);
list3.insert(6);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
print("\n\n Before arrange : ", terminator: "");
//Before arrange nodes
list3.display();
//When linked list Odd nodes are more than Even
list3.arrange_even_odds();
print("\n After arrange  : ", terminator: "");
//After arrange nodes
list3.display();
}
main();``````

#### Output

`````` Before arrange :
Linked List    :     6   4   5   10   3   7   9   2   8
After arrange  :
Linked List    :     6   5   4   3   10   7   2   9   8

Before arrange :
Linked List    :     1   1   4   2   3   1
After arrange  :
Linked List    :     4   1   2   1   3   1

Before arrange :
Linked List    :     2   6   4   8   3   3   2
After arrange  :
Linked List    :     2   3   6   3   4   8   2``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.