# Reverse linked list using recursion

Here given code implementation process.

``````//C Program
#include <stdio.h>

#include <stdlib.h> //for malloc function

struct Node
{
int data;
struct Node *next;
};
void insert(struct Node **head, int value)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Assign data value
node->data = value;
node->next = NULL;
{
}
else
{
//Find last node of linked list
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;
}
}
{
//Base case when need to stop recursion
{
return;
}
//Variable which is hold information of
//current and next node of linked list
struct Node *next_node = NULL;
if (current_node->next != NULL)
{
//When next node exist in linked list
next_node = current_node->next;
}
{
}
if (next_node == NULL)
{
//When get a last node of linked list
}
if (next_node != NULL)
{
//When need to combining of next node linked to previous node
next_node->next = current_node;
}
current_node->next = NULL;
}
int main()
{
//create node pointer
printf("\n Before Reverse : ");
printf("\n After  Reverse : ");
return 0;
}``````

#### Output

`````` Before Reverse : 1  4  3  7  8
After  Reverse : 8  7  3  4  1``````
``````//Java Program
class Node
{
public int data;
public Node next;
//Make a new node
public Node(int data)
{
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
public void insert(int data)
{
//Create  node
Node new_node = new Node(data);
{
this.tail = new_node;
}
else
{
this.tail.next = new_node;
this.tail = new_node;
}
}
//Display element of Node
public void display()
{
if (temp == null)
{
}
while (temp != null)
{
System.out.print(" " + temp.data);
temp = temp.next;
}
}
public void reverse_nodes()
{
//Base case when need to stop recursion
{
return;
}
//Variable which is hold information of
//current and next node of linked list
Node next_node = null;
if (current_node.next != null)
{
//When next node exist in linked list
next_node = current_node.next;
}
{
reverse_nodes();
}
if (next_node == null)
{
//When get a last node of linked list
this.tail = current_node;
}
current_node.next = null;
this.tail.next = current_node;
this.tail = current_node;
}
public static void main(String[] args)
{
obj.insert(1);
obj.insert(4);
obj.insert(3);
obj.insert(7);
obj.insert(8);
System.out.print("\n Before Reverse : ");
obj.display();
obj.reverse_nodes();
System.out.print("\n After  Reverse : ");
obj.display();
}
}``````

#### Output

`````` Before Reverse :  1 4 3 7 8
After  Reverse :  8 7 3 4 1``````
``````//Include header file
#include <iostream>
using namespace std;

//C++ Program
class Node
{
public: int data;
Node * next;
//Make a new node
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
{
Node * tail;
//Class constructors
{
this->tail = NULL;
}
void insert(int data)
{
//Create  node
Node * new_node = new Node(data);
{
this->tail = new_node;
}
else
{
this->tail->next = new_node;
this->tail = new_node;
}
}
//Display element of Node
void display()
{
if (temp == NULL)
{
}
while (temp != NULL)
{
cout << " " << temp->data;
temp = temp->next;
}
}
void reverse_nodes()
{
//Base case when need to stop recursion
{
return;
}
//Variable which is hold information of
//current and next node of linked list
Node * next_node = NULL;
if (current_node->next != NULL)
{
//When next node exist in linked list
next_node = current_node->next;
}
{
this->reverse_nodes();
}
if (next_node == NULL)
{
//When get a last node of linked list
this->tail = current_node;
}
current_node->next = NULL;
this->tail->next = current_node;
this->tail = current_node;
}
};
int main()
{
obj.insert(1);
obj.insert(4);
obj.insert(3);
obj.insert(7);
obj.insert(8);
cout << "\n Before Reverse : ";
obj.display();
obj.reverse_nodes();
cout << "\n After  Reverse : ";
obj.display();
return 0;
}``````

#### Output

`````` Before Reverse : 1  4  3  7  8
After  Reverse : 8  7  3  4  1``````
``````//Include namespace system
using System;
//C# Program
class Node
{
public int data;
public Node next;
//Make a new node
public Node(int data)
{
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
public void insert(int data)
{
//Create  node
Node new_node = new Node(data);
{
this.tail = new_node;
}
else
{
this.tail.next = new_node;
this.tail = new_node;
}
}
//Display element of Node
public void display()
{
if (temp == null)
{
}
while (temp != null)
{
Console.Write(" " + temp.data);
temp = temp.next;
}
}
public void reverse_nodes()
{
//Base case when need to stop recursion
{
return;
}
//Variable which is hold information of
//current and next node of linked list
Node next_node = null;
if (current_node.next != null)
{
//When next node exist in linked list
next_node = current_node.next;
}
{
reverse_nodes();
}
if (next_node == null)
{
//When get a last node of linked list
this.tail = current_node;
}
current_node.next = null;
this.tail.next = current_node;
this.tail = current_node;
}
public static void Main(String[] args)
{
obj.insert(1);
obj.insert(4);
obj.insert(3);
obj.insert(7);
obj.insert(8);
Console.Write("\n Before Reverse : ");
obj.display();
obj.reverse_nodes();
Console.Write("\n After  Reverse : ");
obj.display();
}
}``````

#### Output

`````` Before Reverse :  1 4 3 7 8
After  Reverse :  8 7 3 4 1``````
``````<?php
//Php Program
class Node
{
public \$data;
public \$next;
//Make a new node
function __construct(\$data)
{
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructors
function __construct()
{
\$this->tail = null;
}
public	function insert(\$data)
{
//Create  node
\$new_node = new Node(\$data);
{
\$this->tail = \$new_node;
}
else
{
\$this->tail->next = \$new_node;
\$this->tail = \$new_node;
}
}
//Display element of Node
public	function display()
{
if (\$temp == null)
{
}
while (\$temp != null)
{
echo " ". \$temp->data;
\$temp = \$temp->next;
}
}
public	function reverse_nodes()
{
//Base case when need to stop recursion
{
return;
}
//Variable which is hold information of
//current and next node of linked list
\$next_node = null;
if (\$current_node->next != null)
{
//When next node exist in linked list
\$next_node = \$current_node->next;
}
{
\$this->reverse_nodes();
}
if (\$next_node == null)
{
//When get a last node of linked list
\$this->tail = \$current_node;
}
\$current_node->next = null;
\$this->tail->next = \$current_node;
\$this->tail = \$current_node;
}
}

function main()
{
\$obj->insert(1);
\$obj->insert(4);
\$obj->insert(3);
\$obj->insert(7);
\$obj->insert(8);
echo "\n Before Reverse : ";
\$obj->display();
\$obj->reverse_nodes();
echo "\n After  Reverse : ";
\$obj->display();
}
main();``````

#### Output

`````` Before Reverse :  1 4 3 7 8
After  Reverse :  8 7 3 4 1``````
``````//Node Js Program
class Node
{
//Make a new node
constructor(data)
{
this.data = data;
this.next = null;
}
}
{
//Class constructors
constructor()
{
this.tail = null;
}
insert(data)
{
//Create  node
var new_node = new Node(data);
{
this.tail = new_node;
}
else
{
this.tail.next = new_node;
this.tail = new_node;
}
}
//Display element of Node
display()
{
if (temp == null)
{
}
while (temp != null)
{
process.stdout.write(" " + temp.data);
temp = temp.next;
}
}
reverse_nodes()
{
//Base case when need to stop recursion
{
return;
}
//Variable which is hold information of
//current and next node of linked list
var next_node = null;
if (current_node.next != null)
{
//When next node exist in linked list
next_node = current_node.next;
}
{
this.reverse_nodes();
}
if (next_node == null)
{
//When get a last node of linked list
this.tail = current_node;
}
current_node.next = null;
this.tail.next = current_node;
this.tail = current_node;
}
}

function main()
{
obj.insert(1);
obj.insert(4);
obj.insert(3);
obj.insert(7);
obj.insert(8);
process.stdout.write("\n Before Reverse : ");
obj.display();
obj.reverse_nodes();
process.stdout.write("\n After  Reverse : ");
obj.display();
}
main();``````

#### Output

`````` Before Reverse :  1 4 3 7 8
After  Reverse :  8 7 3 4 1``````
``````# Python 3 Program
# Reverse linked list using recursion
class Node :

# Make a new node
def __init__(self, data) :
self.data = data
self.next = None

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

def insert(self, data) :
# Create  node
new_node = Node(data)
self.tail = new_node
else :
self.tail.next = new_node
self.tail = new_node

# Display element of Node
def display(self) :
if (temp == None) :
print("Empty linked list", end = "")

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

def reverse_nodes(self) :
# Base case when need to stop recursion
return

# Variable which is hold information of
# current and next node of linked list
next_node = None
if (current_node.next != None) :
# When next node exist in linked list
next_node = current_node.next

self.reverse_nodes()

if (next_node == None) :
# When get a last node of linked list
self.tail = current_node

current_node.next = None
self.tail.next = current_node
self.tail = current_node

def main() :
obj.insert(1)
obj.insert(4)
obj.insert(3)
obj.insert(7)
obj.insert(8)
print("\n Before Reverse : ", end = "")
obj.display()
obj.reverse_nodes()
print("\n After  Reverse : ", end = "")
obj.display()

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

#### Output

`````` Before Reverse :   1  4  3  7  8
After  Reverse :   8  7  3  4  1``````
``````# Ruby Program
# Reverse linked list using recursion
class Node

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

# Make a new node
def initialize(data)

self.data = data
self.next = nil
end
end

# Class constructors
def initialize()

self.tail = nil
end
def insert(data)

# Create  node
new_node = Node.new(data)

self.tail = new_node
else

self.tail.next = new_node
self.tail = new_node
end
end
# Display element of Node
def display()

if (temp == nil)

end
while (temp != nil)

print(" ", temp.data)
temp = temp.next
end
end
def reverse_nodes()

# Base case when need to stop recursion

return
end
# Variable which is hold information of
# current and next node of linked list
next_node = nil
if (current_node.next != nil)

# When next node exist in linked list
next_node = current_node.next
end

self.reverse_nodes()
end
if (next_node == nil)

# When get a last node of linked list
self.tail = current_node
end
current_node.next = nil
self.tail.next = current_node
self.tail = current_node
end
end
def main()

obj.insert(1)
obj.insert(4)
obj.insert(3)
obj.insert(7)
obj.insert(8)
print("\n Before Reverse : ")
obj.display()
obj.reverse_nodes()
print("\n After  Reverse : ")
obj.display()
end
main()``````

#### Output

`````` Before Reverse :  1 4 3 7 8
After  Reverse :  8 7 3 4 1``````
``````//Scala Program
class Node(var data: Int,
var next: Node)
{
//Make a new node
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructors
def this()
{
this(null, null);
}
def insert(data: Int): Unit = {
//Create  node
var new_node: Node = new Node(data);
{
this.tail = new_node;
}
else
{
this.tail.next = new_node;
this.tail = new_node;
}
}
//Display element of Node
def display(): Unit = {
if (temp == null)
{
}
while (temp != null)
{
print(" " + temp.data);
temp = temp.next;
}
}
def reverse_nodes(): Unit = {
//Base case when need to stop recursion
{
return;
}
//Variable which is hold information of
//current and next node of linked list
var next_node: Node = null;
if (current_node.next != null)
{
//When next node exist in linked list
next_node = current_node.next;
}
{
reverse_nodes();
}
if (next_node == null)
{
//When get a last node of linked list
this.tail = current_node;
}
current_node.next = null;
this.tail.next = current_node;
this.tail = current_node;
}
}
object Main
{
def main(args: Array[String]): Unit = {
obj.insert(1);
obj.insert(4);
obj.insert(3);
obj.insert(7);
obj.insert(8);
print("\n Before Reverse : ");
obj.display();
obj.reverse_nodes();
print("\n After  Reverse : ");
obj.display();
}
}``````

#### Output

`````` Before Reverse :  1 4 3 7 8
After  Reverse :  8 7 3 4 1``````
``````//Swift Program
class Node
{
var data: Int;
var next: Node? ;
//Make a new node
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructors
init()
{
self.tail = nil;
}
func insert(_ data: Int)
{
//Create  node
let new_node: Node? = Node(data);
{
self.tail = new_node;
}
else
{
self.tail!.next = new_node;
self.tail = new_node;
}
}
//Display element of Node
func display()
{
if (temp == nil)
{
}
while (temp != nil)
{
print(" ", temp!.data, terminator: "");
temp = temp!.next;
}
}
func reverse_nodes()
{
//Base case when need to stop recursion
{
return;
}
//Variable which is hold information of
//current and next node of linked list
var next_node: Node? = nil;
if (current_node!.next != nil)
{
//When next node exist in linked list
next_node = current_node!.next;
}
{
self.reverse_nodes();
}
if (next_node == nil)
{
//When get a last node of linked list
self.tail = current_node;
}
current_node!.next = nil;
self.tail!.next = current_node;
self.tail = current_node;
}
}
func main()
{
obj.insert(1);
obj.insert(4);
obj.insert(3);
obj.insert(7);
obj.insert(8);
print("\n Before Reverse : ", terminator: "");
obj.display();
obj.reverse_nodes();
print("\n After  Reverse : ", terminator: "");
obj.display();
}
main();``````

#### Output

`````` Before Reverse :   1  4  3  7  8
After  Reverse :   8  7  3  4  1``````

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.