- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Program to Display the Nodes of a Linked List in Reverse using Recursion
When it is required to display the elements/nodes in a linked list in reverse order, using recursion method, a method to add values to the linked list, as well as a method to reverse the elements of a Linked List. It would also have a helper method that uses recursion, i.e calls the helper function again and again to compute values.
Below is a demonstration for the same −
class Node: def __init__(self, data): self.data = data self.next = None class my_linked_list: def __init__(self): self.head = None self.last_node = None def add_value(self, my_data): if self.last_node is None: self.head = Node(my_data) self.last_node = self.head else: self.last_node.next = Node(my_data) self.last_node = self.last_node.next def reverse_display(self): self.helper_reverse_display(self.head) def helper_reverse_display(self, curr): if curr is None: return self.helper_reverse_display(curr.next) print(curr.data) my_instance = my_linked_list() n = int(input('How many elements you wish to add ? ')) for i in range(n): data = int(input('Enter the data item : ')) my_instance.add_value(data) print('The reversed linked list is: ') my_instance.reverse_display()
How many elements you wish to add ? 4 Enter the data item : 21 Enter the data item : 34 Enter the data item : 56 Enter the data item : 68 The reversed linked list is: 68 56 34 21
The ‘Node’ class is created.
Another ‘my_linked_list’ class with required attributes is created.
It has an ‘init’ function that is used to initialize the first element, i.e the ‘head’ to ‘None’ and last node to ‘None’.
Another method named ‘add_value’ is defined, that is used to add data to the linked list.
Another method named ‘reverse_display’ is defined that is used to display the linked list data in reverse order on the console.
Another helper function is defined, since recursion needs to be used here.
The ‘reverse_display’ is called in the helper function.
An object of the ‘my_linked_list’ class is created.
The user input is taken for the number of elements in the linked list.
The elements are added to the list.
This range is iterated over, and the methods are called on it to add data.
The element to be searched is taken as user input, and it is searched for.
The index of this element, if found, is displayed on the console.
- Related Articles
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Python Program to Display all the Nodes in a Linked List using Recursion
- Python Program to Print the Alternate Nodes in a Linked List using Recursion
- Program to reverse inner nodes of a linked list in python
- Python Program to Print the Alternate Nodes in a Linked List without using Recursion
- Python program to create a doubly linked list of n nodes and display it in reverse order
- Python program to create a Circular Linked List of n nodes and display it in reverse order
- Print alternate nodes of a linked list using recursion in C++
- C program to display numbers in reverse order using single linked list
- Python Program to Find the Length of the Linked List using Recursion
- Program to reverse a linked list in Python
- Python Program to Find the Length of the Linked List without using Recursion
- Program to swap nodes in a linked list in Python
- Python Program to Reverse a String Using Recursion
- Python Program to Reverse a Stack using Recursion