# Recursively Reversing a linked list (A simple implementation)

• Difficulty Level : Medium
• Last Updated : 31 Jan, 2023

Given pointer to the head node of a linked list, the task is to recursively reverse the linked list. We need to reverse the list by changing links between nodes.

Examples:

```Input : Head of following linked list
1->2->3->4->NULL
Output : Linked list should be changed to,
4->3->2->1->NULL

1->2->3->4->5->NULL
Output : Linked list should be changed to,
5->4->3->2->1->NULL

Input : NULL
Output : NULL

Input  : 1->NULL
Output : 1->NULL```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

We have discussed an iterative and two recursive approaches in previous post on reverse a linked list. In this approach of reversing a linked list by passing a single pointer what we are trying to do is that we are making the previous node of the current node as his next node to reverse the linked list.

1. We return the pointer of next node to his previous(current) node and then make the previous node as the next node of returned node and then return the current node.
2. We first traverse to the last node and make the last node the head node of reversed linked list and then apply the above procedure in a recursive manner.

Implementation:

## C++

 `// Recursive C++ program to reverse``// a linked list``#include ``using` `namespace` `std;` `/* Link list node */``struct` `Node {``    ``int` `data;``    ``struct` `Node* next;``    ``Node(``int` `data)``    ``{``        ``this``->data = data;``        ``next = NULL;``    ``}``};` `struct` `LinkedList {``    ``Node* head;``    ``LinkedList()``    ``{``        ``head = NULL;``    ``}` `    ``/* Function to reverse the linked list */``    ``Node* reverse(Node* node)``    ``{``        ``if` `(node == NULL)``            ``return` `NULL;``        ``if` `(node->next == NULL) {``            ``head = node;``            ``return` `node;``        ``}``        ``Node* node1 = reverse(node->next);``        ``node1->next = node;``        ``node->next = NULL;``        ``return` `node;``    ``}` `    ``/* Function to print linked list */``    ``void` `print()``    ``{``        ``struct` `Node* temp = head;``        ``while` `(temp != NULL) {``            ``cout << temp->data << ``" "``;``            ``temp = temp->next;``        ``}``    ``}` `    ``void` `push(``int` `data)``    ``{``        ``Node* temp = ``new` `Node(data);``        ``temp->next = head;``        ``head = temp;``    ``}``};` `/* Driver program to test above function*/``int` `main()``{``    ``/* Start with the empty list */``    ``LinkedList ll;``    ``ll.push(20);``    ``ll.push(4);``    ``ll.push(15);``    ``ll.push(85);` `    ``cout << ``"Given linked list\n"``;``    ``ll.print();` `    ``ll.reverse(ll.head);` `    ``cout << ``"\nReversed Linked list \n"``;``    ``ll.print();``    ``return` `0;``}`

## Java

 `// Recursive Java program to reverse``// a linked list``import` `java.io.BufferedWriter;``import` `java.io.IOException;``import` `java.io.OutputStreamWriter;``import` `java.util.Scanner;` `public` `class` `ReverseLinkedListRecursive {``    ` `    ``/* Link list node */``    ``static` `class` `Node {``        ``public` `int` `data;``        ``public` `Node next;` `        ``public` `Node(``int` `nodeData) {``            ``this``.data = nodeData;``            ``this``.next = ``null``;``        ``}``    ``}` `    ``static` `class` `LinkedList {``        ``public` `Node head;` `        ``public` `LinkedList() {``            ``this``.head = ``null``;``        ``}` `        ``public` `void` `insertNode(``int` `nodeData) {``            ``Node node = ``new` `Node(nodeData);` `            ``if` `(``this``.head != ``null``) {``                ``node.next = head;``            ``}``            ``this``.head = node;``        ``}``    ``}` `    ``/* Function to print linked list */``    ``public` `static` `void` `printSinglyLinkedList(Node node,``                        ``String sep) ``throws` `IOException {``        ``while` `(node != ``null``) {``            ``System.out.print(String.valueOf(node.data) + sep);``            ``node = node.next;``        ``}``    ``}` `    ``// Complete the reverse function below.``    ``static` `Node reverse(Node head) {``        ``if``(head == ``null``) {``            ``return` `head;``        ``}` `        ``// last node or only one node``        ``if``(head.next == ``null``) {``            ``return` `head;``        ``}` `        ``Node newHeadNode = reverse(head.next);` `        ``// change references for middle chain``        ``head.next.next = head;``        ``head.next = ``null``;` `        ``// send back new head node in every recursion``        ``return` `newHeadNode;``    ``}` `    ``private` `static` `final` `Scanner scanner = ``new` `Scanner(System.in);` `    ``public` `static` `void` `main(String[] args) ``throws` `IOException {``            ``LinkedList llist = ``new` `LinkedList();``        ` `            ``llist.insertNode(``20``);``            ``llist.insertNode(``4``);``            ``llist.insertNode(``15``);``            ``llist.insertNode(``85``);``            ` `            ``System.out.println(``"Given linked list:"``);``            ``printSinglyLinkedList(llist.head, ``" "``);``            ` `            ``System.out.println();``            ``System.out.println(``"Reversed Linked list:"``);``            ``Node llist1 = reverse(llist.head);``            ``printSinglyLinkedList(llist1, ``" "``);` `        ``scanner.close();``    ``}``}`

## Python3

 `# Recursive Python3 program to reverse``# a linked list``import` `math` `# Link list node``class` `Node:``    ``def` `__init__(``self``, data):``        ``self``.data ``=` `data``        ``self``.``next` `=` `None``    ` `def` `LinkedList():``    ``head ``=` `None``        ` `# Function to reverse the linked list``def` `reverse(node):``    ``if` `(node ``=``=` `None``):``        ``return` `node``        ` `    ``if` `(node.``next` `=``=` `None``):``        ``return` `node``        ` `    ``node1 ``=` `reverse(node.``next``)``    ``node.``next``.``next` `=` `node``    ``node.``next` `=` `None``    ``return` `node1` `# Function to print linked list``def` `printList():``    ``temp ``=` `head``    ``while` `(temp !``=` `None``) :``        ``print``(temp.data, end ``=` `" "``)``        ``temp ``=` `temp.``next``        ` `def` `push(head_ref, new_data):``    ``new_node ``=` `Node(new_data)``    ``new_node.data ``=` `new_data``    ``new_node.``next` `=` `head_ref``    ``head_ref ``=` `new_node``    ``return` `head_ref` `# Driver Code``if` `__name__``=``=``'__main__'``:``    ` `    ``# Start with the empty list``    ``head ``=` `LinkedList()``    ``head ``=` `push(head, ``20``)``    ``head ``=` `push(head, ``4``)``    ``head ``=` `push(head, ``15``)``    ``head ``=` `push(head, ``85``)` `    ``print``(``"Given linked list"``)``    ``printList()` `    ``head ``=` `reverse(head)` `    ``print``(``"\nReversed Linked list"``)``    ``printList()``    ` `# This code is contributed by AbhiThakur`

## C#

 `// Recursive C# program to reverse``// a linked list``using` `System;` `public` `class` `ReverseLinkedListRecursive``{``    ` `    ``/* Link list node */``    ``public` `class` `Node``    ``{``        ``public` `int` `data;``        ``public` `Node next;` `        ``public` `Node(``int` `nodeData)``        ``{``            ``this``.data = nodeData;``            ``this``.next = ``null``;``        ``}``    ``}` `    ``class` `LinkedList``    ``{``        ``public` `Node head;` `        ``public` `LinkedList()``        ``{``            ``this``.head = ``null``;``        ``}` `        ``public` `void` `insertNode(``int` `nodeData)``        ``{``            ``Node node = ``new` `Node(nodeData);` `            ``if` `(``this``.head != ``null``)``            ``{``                ``node.next = head;``            ``}``            ``this``.head = node;``        ``}``    ``}` `    ``/* Function to print linked list */``    ``public` `static` `void` `printSinglyLinkedList(Node node,``                        ``String sep)``    ``{``        ``while` `(node != ``null``)``        ``{``            ``Console.Write(node.data + sep);``            ``node = node.next;``        ``}``    ``}` `    ``// Complete the reverse function below.``    ``static` `Node reverse(Node head)``    ``{``        ``if``(head == ``null``)``        ``{``            ``return` `head;``        ``}` `        ``// last node or only one node``        ``if``(head.next == ``null``)``        ``{``            ``return` `head;``        ``}` `        ``Node newHeadNode = reverse(head.next);` `        ``// change references for middle chain``        ``head.next.next = head;``        ``head.next = ``null``;` `        ``// send back new head``        ``// node in every recursion``        ``return` `newHeadNode;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``            ``LinkedList llist = ``new` `LinkedList();``        ` `            ``llist.insertNode(20);``            ``llist.insertNode(4);``            ``llist.insertNode(15);``            ``llist.insertNode(85);``            ` `            ``Console.WriteLine(``"Given linked list:"``);``            ``printSinglyLinkedList(llist.head, ``" "``);``            ` `            ``Console.WriteLine();``            ``Console.WriteLine(``"Reversed Linked list:"``);``            ``Node llist1 = reverse(llist.head);``            ``printSinglyLinkedList(llist1, ``" "``);``    ``}``}` `// This code has been contributed by Rajput-Ji`

## Javascript

 `// Recursive JavaScirpt program to reverse``// a linked list` `// Link List node``class Node{``    ``constructor(data){``        ``this``.data = data;``        ``this``.next = ``null``;``    ``}``}``let head = ``null``;``// Function to reverse the linked list``function` `reverse(node){``    ``if``(node == ``null``) ``return` `null``;``    ` `    ``if``(node.next == ``null``){``        ``head = node;``        ``return` `node;``    ``}``    ` `    ``let node1 = reverse(node.next);``    ``node1.next = node;``    ``node.next = ``null``;``    ``return` `node;``}` `// Function to print linked list``function` `print(){``    ``let temp = head;``    ``while``(temp != ``null``){``        ``console.log(temp.data + ``" "``);``        ``temp = temp.next;``    ``}``}` `function` `push(data){``    ``let temp = ``new` `Node(data);``    ``temp.next = head;``    ``head = temp;``}` `// Driver Code``push(20);``push(4);``push(15);``push(85);` `console.log(``"Given Linked List "``);``print();` `reverse(head)` `console.log(``"\nReversed Linked List"``);``print();` `// This code is contributed by Yash Agarwal(yashagarwal2852002)`

Output

```Given linked list
85 15 4 20