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>

//Linked List Node
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;
}
//Add new node at end of linked list 
void insert(struct Node **head, int data)
{
	struct Node *node = create_node(data);
	if ( *head == NULL)
	{
		*head = node;
	}
	else
	{
		struct Node *temp = *head;
		//Find last node
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
		//Add node at last possition
		temp->next = node;
	}
}
//Display linked list element
void display(struct Node *head)
{
	if (head == NULL)
	{
		printf("\nEmpty linked list\n");
		return;
	}
	struct Node *temp = head;
	//iterating linked list elements
	while (temp != NULL)
	{
		printf("  %d", temp->data);
		//visit to next node
		temp = temp->next;
	}
}
//Arrange alternative even and odd nodes in linked list
struct Node *arrange_even_odds(struct Node *head)
{
	//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;
	struct Node *auxiliary = head;
	head = 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
		head = even_node;
	}
	else
	{
		//When only odd element exists in linked list
		head = odd_node;
	}
	//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;
	}
	return head;
}
int main()
{
	struct Node *list1 = NULL;
	struct Node *list2 = NULL;
	struct Node *list3 = NULL;
	//create linked list
	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);
	//create linked list
	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);
	//create linked list
	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

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructors
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			System.out.print("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		System.out.print("\n Linked List    :  ");
		//iterating linked list elements
		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;
		Node auxiliary = this.head;
		this.head = 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
			this.head = even_node;
		}
		else
		{
			//When only odd element exists in linked list
			this.head = odd_node;
		}
		//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)
	{
		MyLinkedList list1 = new MyLinkedList();
		MyLinkedList list2 = new MyLinkedList();
		MyLinkedList list3 = new MyLinkedList();
		//create linked list
		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();
		//create linked list
		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();
		//create linked list
		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

//Node of LinkedList
class Node
{
	public: 
    int data;
	Node *next;
	Node(int data)
	{
		//Set node value
		this->data = data;
		this->next = NULL;
	}
};
class MyLinkedList
{
	public: 
    Node *head;
	Node *tail;
	//Class constructors
	MyLinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	//insert node at last of linke list
	void insert(int data)
	{
		//Create a node
		Node *node = new Node(data);
		if (this->head == NULL)
		{
			//When linked list empty add first node
			this->head = node;
			this->tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this->tail->next = node;
			this->tail = node;
		}
	}
	//Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\nEmpty linked list\n";
			return;
		}
		Node *temp = this->head;
		cout << "\n Linked List    :  ";
		//iterating linked list elements
		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;
		Node *auxiliary = this->head;
		this->head = 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
			this->head = even_node;
		}
		else
		{
			//When only odd element exists in linked list
			this->head = odd_node;
		}
		//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()
{
	MyLinkedList list1 = MyLinkedList();
	MyLinkedList list2 = MyLinkedList();
	MyLinkedList list3 = MyLinkedList();
	//create linked list
	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();
	//create linked list
	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();
	//create linked list
	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

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructors
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.Write("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		Console.Write("\n Linked List    :  ");
		//iterating linked list elements
		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;
	
		Node auxiliary = this.head;
		this.head = 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
			this.head = even_node;
		}
		else
		{
			//When only odd element exists in linked list
			this.head = odd_node;
		}
		//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)
	{
		MyLinkedList list1 = new MyLinkedList();
		MyLinkedList list2 = new MyLinkedList();
		MyLinkedList list3 = new MyLinkedList();
		//create linked list
		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();
		//create linked list
		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();
		//create linked list
		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

//Node of LinkedList
class Node
{
	public $data;
	public $next;

	function __construct($data)
	{
		//Set node value
		$this->data = $data;
		$this->next = null;
	}
}
class MyLinkedList
{
	public $head;
	public $tail;
	//Class constructors
	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	//insert node at last of linke list
	public	function insert($data)
	{
		//Create a node
		$node = new Node($data);
		if ($this->head == null)
		{
			//When linked list empty add first node
			$this->head = $node;
			$this->tail = $node;
		}
		else
		{
			//Add new node at end of linked list
			$this->tail->next = $node;
			$this->tail = $node;
		}
	}
	//Display linked list element
	public	function display()
	{
		if ($this->head == null)
		{
			echo "\nEmpty linked list\n";
			return;
		}
		$temp = $this->head;
		echo "\n Linked List    :  ";
		//iterating linked list elements
		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;
		$auxiliary = $this->head;
		$this->head = 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
			$this->head = $even_node;
		}
		else
		{
			//When only odd element exists in linked list
			$this->head = $odd_node;
		}
		//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 = new MyLinkedList();
	$list2 = new MyLinkedList();
	$list3 = new MyLinkedList();
	//create linked list
	$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();
	//create linked list
	$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();
	//create linked list
	$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

//Node of LinkedList
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	//Class constructors
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	insert(data)
	{
		//Create a node
		var node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	display()
	{
		if (this.head == null)
		{
			process.stdout.write("\nEmpty linked list\n");
			return;
		}
		var temp = this.head;
		process.stdout.write("\n Linked List    :  ");
		//iterating linked list elements
		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;
		var auxiliary = this.head;
		this.head = 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
			this.head = even_node;
		}
		else
		{
			//When only odd element exists in linked list
			this.head = odd_node;
		}
		//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()
{
	var list1 = new MyLinkedList();
	var list2 = new MyLinkedList();
	var list3 = new MyLinkedList();
	//create linked list
	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();
	//create linked list
	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();
	//create linked list
	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

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

class MyLinkedList :
	
	# Class constructors
	def __init__(self) :
		self.head = None
		self.tail = None
	
	# insert node at last of linke list
	def insert(self, data) :
		# Create a node
		node = Node(data)
		if (self.head == None) :
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else :
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		
	
	# Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\nEmpty linked list\n", end = "")
			return
		
		temp = self.head
		print("\n Linked List    :  ", end = "")
		# iterating linked list elements
		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
		auxiliary = self.head
		self.head = 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
			self.head = even_node
		else :
			# When only odd element exists in linked list
			self.head = odd_node
		
		# 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 = MyLinkedList()
	list2 = MyLinkedList()
	list3 = MyLinkedList()
	# create linked list
	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()
	# create linked list
	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()
	# create linked list
	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

# Node of LinkedList
class Node 

	# Define the accessor and reader of class Node  
	attr_reader :data, :next
	attr_accessor :data, :next
	
	def initialize(data)
	
		# Set node value
		self.data = data
		self.next = nil
	end
end
class MyLinkedList 

	# Define the accessor and reader of class MyLinkedList  
	attr_reader :head, :tail
	attr_accessor :head, :tail
	
	# Class constructors
	def initialize()
	
		self.head = nil
		self.tail = nil
	end
	# insert node at last of linke list
	def insert(data)
	
		# Create a node
		node = Node.new(data)
		if (self.head == nil)
		
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else
		
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		end
	end
	# Display linked list element
	def display()
	
		if (self.head == nil)
		
			print("\nEmpty linked list\n")
			return
		end
		temp = self.head
		print("\n Linked List    :  ")
		# iterating linked list elements
		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
		auxiliary = self.head
		self.head = 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
			self.head = even_node
		else
		
			# When only odd element exists in linked list
			self.head = odd_node
		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 = MyLinkedList.new()
	list2 = MyLinkedList.new()
	list3 = MyLinkedList.new()
	# create linked list
	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()
	# create linked list
	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()
	# create linked list
	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

//Node of LinkedList
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class MyLinkedList(var head: Node,
	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);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			print("\nEmpty linked list\n");
			return;
		}
		var temp: Node = this.head;
		print("\n Linked List    :  ");
		//iterating linked list elements
		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;
		var auxiliary: Node = this.head;
		this.head = 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
			this.head = even_node;
		}
		else
		{
			//When only odd element exists in linked list
			this.head = odd_node;
		}
		//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 = {
		var list1: MyLinkedList = new MyLinkedList();
		var list2: MyLinkedList = new MyLinkedList();
		var list3: MyLinkedList = new MyLinkedList();
		//create linked list
		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();
		//create linked list
		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();
		//create linked list
		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

//Node of LinkedList
class Node
{
    var data: Int;
    var next: Node? ;
    init(_ data: Int)
    {
        //Set node value
        self.data = data;
        self.next = nil;
    }
}
class MyLinkedList
{
    var head: Node? ;
    var tail: Node? ;
    //Class constructors
    init()
    {
        self.head = nil;
        self.tail = nil;
    }
    //insert node at last of linke list
    func insert(_ data: Int)
    {
        //Create a node
        let node: Node? = Node(data);
        if (self.head == nil)
        {
            //When linked list empty add first node
            self.head = node;
            self.tail = node;
        }
        else
        {
            //Add new node at end of linked list
            self.tail!.next = node;
            self.tail = node;
        }
    }
    //Display linked list element
    func display()
    {
        if (self.head == nil)
        {
            print("\nEmpty linked list\n", terminator: "");
            return;
        }
        var temp: Node? = self.head;
        print("\n Linked List    :  ", terminator: "");
        //iterating linked list elements
        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;
        var auxiliary: Node? = self.head;
        self.head = 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
            self.head = even_node;
        }
        else
        {
            //When only odd element exists in linked list
            self.head = odd_node;
        }
        //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()
{
    let list1: MyLinkedList = MyLinkedList();
    let list2: MyLinkedList = MyLinkedList();
    let list3: MyLinkedList = MyLinkedList();
    //create linked list
    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();
    //create linked list
    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();
    //create linked list
    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.

New Comment







© 2021, kalkicode.com, All rights reserved