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 the Nodes of a Linked List in Reverse without using Recursion
When it is required to display the nodes of a linked list in reverse without using the method of recursion, we can use an iterative approach. This involves finding the last unprinted node in each iteration and displaying it.
Below is a demonstration for the same ā
Example
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 reverse_display(self):
end_node = None
while end_node != self.head:
curr = self.head
while curr.next != end_node:
curr = curr.next
print(curr.data)
end_node = curr
def display(self):
curr = self.head
while curr:
print(curr.data, end=" -> " if curr.next else "\n")
curr = curr.next
# Create linked list with sample data
my_list = LinkedList()
sample_data = [43, 67, 87, 12, 34]
for data in sample_data:
my_list.add_value(data)
print("Original linked list:")
my_list.display()
print("Reversed linked list:")
my_list.reverse_display()
Original linked list: 43 -> 67 -> 87 -> 12 -> 34 Reversed linked list: 34 12 87 67 43
How It Works
The reverse_display() method uses a two-pointer approach:
end_node ā Keeps track of where to stop in each iteration
curr ā Traverses from head to just before end_node
In each iteration, it finds the last unprinted node and displays it
The process continues until all nodes are printed in reverse order
Alternative Approach Using Stack
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def add_value(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def reverse_display_stack(self):
stack = []
curr = self.head
# Push all elements to stack
while curr:
stack.append(curr.data)
curr = curr.next
# Pop and print (LIFO gives reverse order)
while stack:
print(stack.pop())
# Example usage
my_list = LinkedList()
data_items = [34, 12, 87, 67, 43] # Adding in reverse for head insertion
for data in data_items:
my_list.add_value(data)
print("Reversed using stack approach:")
my_list.reverse_display_stack()
Reversed using stack approach: 34 12 87 67 43
Comparison
| Method | Time Complexity | Space Complexity | Description |
|---|---|---|---|
| Iterative (Two-pointer) | O(n²) | O(1) | No extra space, but slower |
| Stack Approach | O(n) | O(n) | Uses extra space but faster |
Conclusion
Both methods successfully display linked list nodes in reverse without recursion. The stack approach is more efficient with O(n) time complexity, while the iterative method saves memory with O(1) space complexity.
