# Reverse the group of even elements in a Linked List

Here given code implementation process.

``````//C Program
//Reverse the group of even elements in a Linked List
#include <stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node
{
int data;
struct Node*next;
};

//insert Node element
{
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL)
{
printf("Memory overflow\n");
}
else
{
node->data=value;
node->next=NULL;
{
}
else
{
//find last node
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
}
}
//Display element of Node
void display(struct Node*temp)
{

if(temp==NULL)
{
}
while(temp!=NULL)
{
printf("%d  ",temp->data);
temp=temp->next;
}
}
//Reversing the all adjacent Even value nodes
{

while(temp!=NULL)
{
if(temp->data%2==0)
{
current=temp;
temp=temp->next;

current->next=prev;
prev=current;
}
else
{
break;
}
}

}
{

while(temp!=NULL && temp->next != NULL)
{
if(temp->data%2 == temp->next->data%2)
{
if(auxiliary==NULL)
{
}
else
{
//Modify the intermediate nodes of linked list
auxiliary->next=reverse(temp);
}
auxiliary=temp;
}
else
{
auxiliary=temp;
}

temp=temp->next;

}

}
int main()
{
//create node pointer

//display all node

printf("\n");

return 0;
}``````

#### Output

``````8  2  6  3  10  6  12  4  5  2
6  2  8  3  4  12  6  10  5  2``````
``````//C++ Program
//Reverse the group of even elements in a Linked List
#include <iostream>
using namespace std;

//Create structure
struct Node
{
int data;
struct Node*next;
};
{

public:
void insert(int);
void display();
void reverse_even();
Node* reverse(Node*);
};
{
}
//insert node at end of linked list
{
//Create dynamic node
Node*node=new Node;
if(node==NULL)
{
cout<<"Memory overflow\n";
}else
{
node->data=value;
node->next=NULL;
{
//base condition
}else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
}
}
//display all node value in linked list
{
{
}
else
{
while(temp!=NULL)
{
//print node value
cout<<temp->data<<" ";
temp=temp->next;
}
}
}
{

while(temp!=NULL)
{
if(temp->data%2==0)
{
current=temp;
temp=temp->next;

current->next=prev;
prev=current;
}
else
{
break;
}
}

}
{

cout<<"\nReverse group of Even node elements"<<endl;

while(temp!=NULL && temp->next != NULL)
{
if(temp->data%2 == temp->next->data%2)
{
if(auxiliary==NULL)
{
}
else
{
//Modify the intermediate nodes of linked list
auxiliary->next=reverse(temp);
}
auxiliary=temp;
}
else
{
auxiliary=temp;
}

temp=temp->next;

}
}
int main()
{
//create object

obj.insert(8);
obj.insert(2);
obj.insert(6);
obj.insert(3);
obj.insert(10);
obj.insert(6);
obj.insert(12);
obj.insert(4);
obj.insert(5);
obj.insert(2);

//display all node
obj.display();

obj.reverse_even();

//display all node
obj.display();
return 0;
}``````

#### Output

``````Linked List : 8 2 6 3 10 6 12 4 5 2
Reverse group of Even node elements
Linked List : 6 2 8 3 4 12 6 10 5 2 ``````
``````//Java program
//Reverse the group of even elements in a Linked List
{

static class Node
{
int data;
Node next;
}
//Class constructors
{
}
//insert  element
public void insert(int value)
{
//Create  node
Node node=new Node();
node.data=value;
node.next=null;
else
{
//find last node
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=node;
}

}
public void display()
{
{
while(temp!=null)
{
System.out.print("  "+temp.data);
temp=temp.next;
}
System.out.println();
}
else
{
}
}
{

while(temp!=null)
{
if(temp.data%2==0)
{
current=temp;
temp=temp.next;

current.next=prev;
prev=current;
}
else
{
break;
}
}

}
public void  reverse_even()
{

System.out.println("Reverse group of Even node elements");

while(temp!=null && temp.next != null)
{
if(temp.data%2 == temp.next.data%2)
{
if(auxiliary==null)
{
}
else
{
//Modify the intermediate nodes of linked list
auxiliary.next=reverse(temp);
}
auxiliary=temp;
}
else
{
auxiliary=temp;
}

temp=temp.next;

}
}

public static void main(String[] args)
{

obj.insert(8);
obj.insert(2);
obj.insert(6);
obj.insert(3);
obj.insert(10);
obj.insert(6);
obj.insert(12);
obj.insert(4);
obj.insert(5);
obj.insert(2);

//display all node
obj.display();

obj.reverse_even();

//display all node
obj.display();

}
}``````

#### Output

``````Linked List Element :  8  2  6  3  10  6  12  4  5  2
Reverse group of Even node elements
Linked List Element :  6  2  8  3  4  12  6  10  5  2
``````
``````#Python Program
#Reverse the group of even elements in a Linked List
class Node:
def __init__(self,data):
self.data=data
self.next=None

def __init__(self):

#Assign default value

def insert(self,data):

node=Node(data)

else:

#find middle node of linked list
while(temp!=None and temp.next!=None ):
temp=temp.next

temp.next=node

def display(self):
while(temp!=None):
print(temp.data,end=" ")
temp=temp.next
print()

return None

prev=None
current=None

while(temp!=None):

if(temp.data%2==0):

current=temp
temp=temp.next

current.next=prev
prev=current

else :
break

def reverse_even(self):

return

print("Reverse group of Even node elements")
auxiliary=None

while(temp!=None  and  temp.next != None):

if(temp.data%2 == temp.next.data%2):

if(auxiliary==None):

else:
#Modify the intermediate nodes of linked list
auxiliary.next=self.reverse(temp)

auxiliary=temp

else:
auxiliary=temp

temp=temp.next

def main():
obj.insert(8)
obj.insert(2)
obj.insert(6)
obj.insert(3)
obj.insert(10)
obj.insert(6)
obj.insert(12)
obj.insert(4)
obj.insert(5)
obj.insert(2)

#display all node
obj.display()

obj.reverse_even()

#display all node
obj.display()

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

#### Output

``````8 2 6 3 10 6 12 4 5 2
Reverse group of Even node elements
6 2 8 3 4 12 6 10 5 2
``````
``````//C# program
//Reverse the group of even elements in a Linked List
using System;
public class Node
{
public int data;
public Node next;
}

{

//Class constructors
{
}
//insert  element
public void insert(int value)
{
//Create  node
Node node=new Node();
node.data=value;
node.next=null;
else
{
//find last node
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=node;
}

}
public void display()
{
{
while(temp!=null)
{
Console.Write("  "+temp.data);
temp=temp.next;
}
Console.WriteLine();
}
else
{
}
}
{

while(temp!=null)
{
if(temp.data%2==0)
{
current=temp;
temp=temp.next;

current.next=prev;
prev=current;
}
else
{
break;
}
}

}
public void  reverse_even()
{

Console.WriteLine("Reverse group of Even node elements");

while(temp!=null && temp.next != null)
{
if(temp.data%2 == temp.next.data%2)
{
if(auxiliary==null)
{
}
else
{
//Modify the intermediate nodes of linked list
auxiliary.next=reverse(temp);
}
auxiliary=temp;
}
else
{
auxiliary=temp;
}

temp=temp.next;

}
}

public static void Main(String[] args)
{

obj.insert(8);
obj.insert(2);
obj.insert(6);
obj.insert(3);
obj.insert(10);
obj.insert(6);
obj.insert(12);
obj.insert(4);
obj.insert(5);
obj.insert(2);

//display all node
obj.display();

obj.reverse_even();

//display all node
obj.display();

}
}``````

#### Output

``````Linked List Element :  8  2  6  3  10  6  12  4  5  2
Reverse group of Even node elements
Linked List Element :  6  2  8  3  4  12  6  10  5  2
``````
``````<?php
//Php program
//Reverse the group of even elements in a Linked List
class Node
{
public \$data;
public \$next;
function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}
{

function __construct()
{
}
/*
* Append the Given data value at end of linked list
* Fun : insert
* Parm: data value
*@return None
*/
function insert(\$data)
{
\$newNode=new Node(\$data);

{
}
else
{

//find last node of linked list
while(\$temp->next!=NULL)
{
\$temp=\$temp->next;
}
\$temp->next=\$newNode;
}
}
//Display all inserted node in linked list
function display()
{
{
}
else
{
while(\$temp!=NULL)
{
//display node value
echo "  ".\$temp->data;
\$temp=\$temp->next; //visit to next node
}
}
}
{

\$prev=NULL;
\$current=NULL;

while(\$temp!=NULL)
{
if(\$temp->data%2==0)
{
\$current=\$temp;
\$temp=\$temp->next;

\$current->next=\$prev;
\$prev=\$current;
}
else
{
break;
}
}

}
function reverse_even()
{

{
return;
}

echo "\nReverse group of Even node elements";
\$auxiliary=NULL;

while(\$temp!=NULL && \$temp->next != NULL)
{
if(\$temp->data%2 == \$temp->next->data%2)
{
if(\$auxiliary==NULL)
{
}
else
{
//Modify the intermediate nodes of linked list
\$auxiliary->next=\$this->reverse(\$temp);
}
\$auxiliary=\$temp;
}
else
{
\$auxiliary=\$temp;
}

\$temp=\$temp->next;

}
}
}
function main()
{
//Make a object of LinkedList class

\$obj->insert(8);
\$obj->insert(2);
\$obj->insert(6);
\$obj->insert(3);
\$obj->insert(10);
\$obj->insert(6);
\$obj->insert(12);
\$obj->insert(4);
\$obj->insert(5);
\$obj->insert(2);

//display all node
\$obj->display();

\$obj->reverse_even();

//display all node
\$obj->display();
}
main();
?>``````

#### Output

``````Linked List :  8  2  6  3  10  6  12  4  5  2
Reverse group of Even node elements
Linked List :  6  2  8  3  4  12  6  10  5  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.