Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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, we need a method to add values to the linked list and a recursive helper method that calls itself repeatedly to print the values. The recursive approach provides an elegant way to traverse the entire linked list.
Below is a demonstration for the same −
Node and LinkedList Classes
First, we define the basic structure of our linked list ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.last_node = None
def add_value(self, data):
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
Recursive Display Method
The recursive method traverses the linked list by calling itself with the next node until it reaches the end ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.last_node = None
def add_value(self, data):
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
def print_list(self):
self.recursive_print(self.head)
def recursive_print(self, current):
if current is None:
return
print(current.data)
self.recursive_print(current.next)
# Create linked list and add elements
my_list = LinkedList()
elements = [34, 67, 12, 89]
for element in elements:
my_list.add_value(element)
print("The linked list:")
my_list.print_list()
The linked list: 34 67 12 89
How Recursion Works
The recursive function works as follows:
-
Base Case: If current node is
None, stop recursion - Process: Print current node's data
- Recursive Call: Call the function with the next node
Alternative: Reverse Order Display
We can also print the linked list in reverse order by changing the order of operations ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.last_node = None
def add_value(self, data):
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
def print_reverse(self):
self.recursive_reverse_print(self.head)
def recursive_reverse_print(self, current):
if current is None:
return
self.recursive_reverse_print(current.next)
print(current.data)
# Create linked list and add elements
my_list = LinkedList()
elements = [34, 67, 12, 89]
for element in elements:
my_list.add_value(element)
print("The linked list in reverse:")
my_list.print_reverse()
The linked list in reverse: 89 12 67 34
Conclusion
Recursion provides an elegant solution for traversing linked lists. The recursive method calls itself with the next node until reaching the end, making the code clean and easy to understand compared to iterative approaches.
