Python Program to Read a Linked List in Reverse

This is a Python program to read a linked list in reverse.

Problem Description

The program creates a singly linked list and allows the user to enter data items to the list in reverse.

Problem Solution

1. Create a class Node with instance variables data and next.
2. Create a class LinkedList with instance variable head.
3. The variable head points to the first element in the singly linked list.
4. Define methods insert_at_beg and display.
5. The method insert_at_beg inserts a node at the first position of the list.
6. The method display traverses the list from the first node and prints the data of each node.
7. Create an instance of LinkedList and prepend the data items input by the user.

Program/Source Code

Here is the source code of a Python program to read a linked list in reverse. 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
 
    def insert_at_beg(self, new_node):
        if self.head is None:
            self.head = new_node
        else:
            new_node.next = self.head
            self.head = new_node
 
    def display(self):
        current = self.head
        while current:
            print(current.data, end = ' ')
            current = current.next
 
 
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: '))
    node = Node(data)
    a_llist.insert_at_beg(node)
 
print('The linked list: ', end = '')
a_llist.display()
Program Explanation

1. An instance of LinkedList is created.
2. The user is prompted to provide data items for the list.
3. The insert_at_beg method is used to enter the data to the list in reverse.

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

Sanfoundry Global Education & Learning Series – 1000 Python Programs.

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

Free 30-Day Python Certification Bootcamp is Live. Join Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.