- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Display all the Nodes in a Linked List using Recursion
When it is required to display the elements/nodes in a linked list, using recursion method, a method to add values to the linked list, and a method to print 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 print the values.
Below is a demonstration for the same −
Example
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 print_it(self): self.helper_print(self.head) def helper_print(self, curr): if curr is None: return print(curr.data) self.helper_print(curr.next) my_instance = my_linked_list() n = int(input('How many elements you wish to add ? ')) for i in range(n): data = int(input('Enter a data item : ')) my_instance.add_value(data) print('The linked list: ') my_instance.print_it()
Output
How many elements you wish to add ? 4 Enter a data item : 34 Enter a data item : 67 Enter a data item : 12 Enter a data item : 89 The linked list: 34 67 12 89
Explanation
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 ‘print_it’ is defined that calls the helper method to display the linked list data on the console.
Another method named ‘helper_print’ is defined that is used to display the linked list data on the console.
This helper function is defined, since recursion needs to be used here.
An object of the ‘my_linked_list’ class is created.
The user input is taken for the number of elements in the linked list.
This range is iterated over, and the methods are called on it to add data.
This is displayed on the console using the ‘print_it’ method.
- Related Articles
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Python Program to Print the Alternate Nodes in a Linked List using Recursion
- Python Program to Print the Alternate Nodes in a Linked List without using Recursion
- Print alternate nodes of a linked list using recursion in C++
- Python Program to Find the Length of the Linked List using Recursion
- Program to swap nodes in a linked list in Python
- Python Program to Find the Length of the Linked List without using Recursion
- Python Program to Search for an Element in the Linked List without using Recursion
- Program to reverse inner nodes of a linked list in python
- Program to delete n nodes after m nodes from a linked list in Python
- 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
- Program to remove all nodes of a linked list whose value is same as in Python
- Python Program to Create a Linked List & Display the Elements in the List
