Recursively Reversing a linked list (A simple implementation)
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 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.
- Print reverse of a Linked List without actually reversing
- Find middle of singly linked list Recursively
- Linked List Implementation in C#
- Queue - Linked List Implementation
- Implementation of Deque using doubly linked list
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Create new linked list from two given linked list with greater element at each node
- Convert singly linked list into circular linked list
- Merge a linked list into another linked list at alternate positions
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Difference between Singly linked list and Doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Check if a linked list is Circular Linked List
- Convert Singly Linked List to XOR Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
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.
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 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(); } } |
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 |
Given linked list 85 15 4 20 Reversed Linked list 20 4 15 85
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



