# Reverse alternate K nodes in a Linked List

Here given code implementation process.

``````//C Program
//Reverse alternate K nodes 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;
}
}

{
{
}
printf("\nReverse alternate %d nodes",k);

struct Node*auxiliary=NULL,
*back=NULL,
*first=NULL;
int move=1, size=0;
while(temp!=NULL)
{
size++;

if(size==k)
{

if(move==1)
{

temp=temp->next;
back=temp;
root=current;

{
current=current->next;
}
while(current != NULL && size > 0)
{

auxiliary = current->next;
current->next = back;
back = current;
current = auxiliary;
size--;
}
{
}
else
{
root->next=back;
}
move=0;
}
else
{
current=temp;
temp=temp->next;
move=1;
size=0;
}
}
else
{
temp=temp->next;
}
}

}

int main()
{
//Create node pointer

//display all node

int k=3;

printf("\n");

return 0;
}``````

#### Output

``````1  2  3  4  5  6  7  8  9  10
Reverse alternate 3 nodes
3  2  1  4  5  6  9  8  7  10``````
``````//C++ Program
//Reverse alternate K nodes 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_k(int);
};
{
}
//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;
}
}
}
{
{
return;
}
cout<<"\nReverse alternate "<<k <<" nodes"<<endl;

Node*auxiliary=NULL,
*back=NULL,
*first=NULL;
int move=1, size=0;
while(temp!=NULL)
{
size++;

if(size==k)
{

if(move==1)
{

temp=temp->next;
back=temp;
root=current;

{
current=current->next;
}
while(current != NULL && size > 0)
{

auxiliary = current->next;
current->next = back;
back = current;
current = auxiliary;
size--;
}
{
}
else
{
root->next=back;
}
move=0;
}
else
{
current=temp;
temp=temp->next;
move=1;
size=0;
}
}
else
{
temp=temp->next;
}
}
}
int main()
{
//create object

obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
obj.insert(8);
obj.insert(9);
obj.insert(10);
//display all node
obj.display();

int k=3;

obj.reverse_k(k);

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

#### Output

``````Linked List : 1 2 3 4 5 6 7 8 9 10
Reverse alternate 3 nodes
Linked List : 3 2 1 4 5 6 9 8 7 10``````
``````//Java program
//Reverse alternate K nodes 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
{
}
}
public void reverse_k(int k)
{
{
return;
}
System.out.println("Reverse alternate "+k+" nodes");

Node auxiliary=null,
back=null,
first=null;
int move=1, size=0;
while(temp!=null)
{
size++;

if(size==k)
{

if(move==1)
{

temp=temp.next;
back=temp;
root=current;

{
current=current.next;
}
while(current != null && size > 0)
{

auxiliary = current.next;
current.next = back;
back = current;
current = auxiliary;
size--;
}
{
}
else
{
root.next=back;
}
move=0;
}
else
{
current=temp;
temp=temp.next;
move=1;
size=0;
}
}
else
{
temp=temp.next;
}
}
}

public static void main(String[] args)
{

obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
obj.insert(8);
obj.insert(9);
obj.insert(10);
//display all node
obj.display();

int k=3;

obj.reverse_k(k);
//display all node
obj.display();
}
}``````

#### Output

``````Linked List Element :  1  2  3  4  5  6  7  8  9  10
Reverse alternate 3 nodes
Linked List Element :  3  2  1  4  5  6  9  8  7  10
``````
``````#Python Program
#Reverse alternate K nodes 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()

def reverse_k(self,k):
return

print("Reverse alternate",k,"nodes")

auxiliary=None
back=None
first=None
move=1
size=0
while(temp!=None):
size+=1

if(size==k):

if(move==1):

temp=temp.next
back=temp
root=current

current=current.next

while(current != None  and  size > 0):

auxiliary = current.next
current.next = back
back = current
current = auxiliary
size-=1

else:
root.next=back

move=0

else:
current=temp
temp=temp.next
move=1
size=0

else :
temp=temp.next

def main():
obj.insert(1)
obj.insert(2)
obj.insert(3)
obj.insert(4)
obj.insert(5)
obj.insert(6)
obj.insert(7)
obj.insert(8)
obj.insert(9)
obj.insert(10)
#display all node
obj.display()

k=3

obj.reverse_k(k)
#display all node
obj.display()

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

#### Output

``````1 2 3 4 5 6 7 8 9 10
Reverse alternate 3 nodes
3 2 1 4 5 6 9 8 7 10
``````
``````//C# program
//Reverse alternate K nodes 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
{
}
}
public void reverse_k(int k)
{
{
return;
}
Console.WriteLine("Reverse alternate "+k+" nodes");

Node auxiliary=null,
back=null,
first=null;
int move=1, size=0;
while(temp!=null)
{
size++;

if(size==k)
{

if(move==1)
{

temp=temp.next;
back=temp;
root=current;

{
current=current.next;
}
while(current != null && size > 0)
{

auxiliary = current.next;
current.next = back;
back = current;
current = auxiliary;
size--;
}
{
}
else
{
root.next=back;
}
move=0;
}
else
{
current=temp;
temp=temp.next;
move=1;
size=0;
}
}
else
{
temp=temp.next;
}
}
}

public static void Main(String[] args)
{

obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
obj.insert(8);
obj.insert(9);
obj.insert(10);
//display all node
obj.display();

int k=3;

obj.reverse_k(k);
//display all node
obj.display();
}
}``````

#### Output

``````Linked List Element :  1  2  3  4  5  6  7  8  9  10
Reverse alternate 3 nodes
Linked List Element :  3  2  1  4  5  6  9  8  7  10
``````
``````<?php
//Php program
//Reverse alternate K nodes 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
}
}
}
function reverse_k(\$k)
{
{
return;
}
echo "\nReverse alternate ".\$k ." nodes";

\$auxiliary=NULL;
\$back=NULL;
\$first=NULL;
\$move=1;
\$size=0;
while(\$temp!=NULL)
{
\$size++;

if(\$size==\$k)
{

if(\$move==1)
{

\$temp=\$temp->next;
\$back=\$temp;
\$root=\$current;

{
\$current = \$current->next;
}
while(\$current != NULL && \$size > 0)
{

\$auxiliary = \$current->next;
\$current->next = \$back;
\$back = \$current;
\$current = \$auxiliary;
\$size--;
}
{
}
else
{
\$root->next=\$back;
}
\$move=0;
}
else
{
\$current=\$temp;
\$temp=\$temp->next;
\$move=1;
\$size=0;
}
}
else
{
\$temp=\$temp->next;
}
}
}
}
function main()
{
//Make a object of LinkedList class

\$obj->insert(1);
\$obj->insert(2);
\$obj->insert(3);
\$obj->insert(4);
\$obj->insert(5);
\$obj->insert(6);
\$obj->insert(7);
\$obj->insert(8);
\$obj->insert(9);
\$obj->insert(10);
//display all node
\$obj->display();

\$k=3;

\$obj->reverse_k(\$k);

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

#### Output

``````Linked List :  1  2  3  4  5  6  7  8  9  10
Reverse alternate 3 nodes
Linked List :  3  2  1  4  5  6  9  8  7  10``````

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.