Python Program to Print All Nodes of Linked List using Recursion

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

Problem Description

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

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 methods display and display_helper.
5. display calls display_helper to display the linked list recursively.
6. Create an instance of LinkedList, append data to it and display the list.

Program/Source Code

Here is the source code of a Python program to display the nodes of a linked list 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(self):
        self.display_helper(self.head)
 
    def display_helper(self, current):
        if current is None:
            return
 
        print(current.data, end = ' ')
        self.display_helper(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: '))
    a_llist.append(data)
 
print('The linked list: ', end = '')
a_llist.display()
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 by calling the method display.

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

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.