Recursively Reversing a linked list (A simple implementation)

24 Jun, 2022
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

Input : Head of following linked list  
       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

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.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 returning the current node.We first traverse till the last node and making the last node as the head node of reversed linked list and then applying the above procedure in the recursive manner.C++JavaPython3C#C++// Recursive C++ program to reverse// a linked list#include <iostream>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 listimport 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 listimport 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 Codeif __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 AbhiThakurC#// Recursive C# program to reverse// a linked listusing 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 Output:Given linked list
85 15 4 20 
Reversed Linked list 
20 4 15 85
My Personal Notes
arrow_drop_upSave
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.
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 returning the current node.We first traverse till the last node and making the last node as the head node of reversed linked list and then applying the above procedure in the recursive manner.
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 returning the current node.
We first traverse till the last node and making the last node as the head node of reversed linked list and then applying the above procedure in the recursive manner.
C++
Java
Python3
C#
// Recursive C++ program to reverse// a linked list#include <iostream>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;}
// Recursive Java program to reverse// a linked listimport 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();    }}
# Recursive Python3 program to reverse# a linked listimport 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 Codeif __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
// Recursive C# program to reverse// a linked listusing 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 
Given linked list
85 15 4 20 
Reversed Linked list 
20 4 15 85

Saurav_Satrawal
Rajput-Ji
abhaysingh290895
Reverse
Linked List
Recursion
Linked List
Recursion
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.