Python Program to Reverse a Linked List without Recursion

This is a Python program to display the nodes of a linked list in reverse without using recursion.

Problem Description

The program creates a linked list using data items input from the user and displays it in reverse.

Problem Solution

1. Create a class Node.
2. Create a class LinkedList.
3. Define methods append and display inside the class LinkedList to append data and display the linked list respectively.
4. Define the method display_reversed.
5. display_reversed uses the variable end_node to keep track of the last node that was printed. The variable current then iterates through the list until it reaches the node before end_node. That node is then displayed.
6. Create an instance of LinkedList, append data to it and display the list in reverse order.

Program/Source Code

Here is the source code of a Python program to display the nodes of a linked list in reverse without using recursion. The program output is shown below.

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 append(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 display_reversed(self):
        end_node = None
 
        while end_node != self.head:
            current = self.head
            while current.next != end_node:
                current = current.next
            print(current.data, end = ' ')
            end_node = current
 
a_llist = LinkedList()
n = int(input('How many elements would you like to add? '))
for i in range(n):
    data = int(input('Enter data item: '))
    a_llist.append(data)
 
print('The reversed linked list: ', end = '')
a_llist.display_reversed()
Program Explanation

1. An instance of LinkedList is created.
2. The user is asked for the number of elements they would like to add. This is stored in n.
3. Using a loop, data from the user is appended to the linked list n times.
4. The linked list is displayed in reverse by calling the method display_reversed.

advertisement
Runtime Test Cases
Case 1:
How many elements would you like to add? 4
Enter data item: 7
Enter data item: 2
Enter data item: 1
Enter data item: 9
The reversed linked list: 9 1 2 7 
 
Case 2:
How many elements would you like to add? 1
Enter data item: 5
The reversed linked list: 5 
 
Case 3:
How many elements would you like to add? 2
Enter data item: 3
Enter data item: 1
The reversed linked list: 1 3

Sanfoundry Global Education & Learning Series – 1000 Python Programs.

If you wish to look at all Python Programming examples, go to 1000 Python Programs.

🎓 Free Certifications on 300 subjects are live for December 2025. Register Now!

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.