Python Program to Print Alternate Nodes of a Linked List using Recursion

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

Problem Description

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

Problem Solution

1. Create a class Node.
2. Create a class LinkedList.
3. Define method append inside the class LinkedList to append data to the linked list.
4. Define methods alternate and alternate_helper.
5. alternate calls alternate_helper to print the alternate nodes of the linked list recursively.
6. Create an instance of LinkedList and prompt the user for its elements.
7. Display the alternate data items of the linked list.

Program/Source Code

Here is the source code of a Python program to print the alternate 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 alternate(self):
        self.alternate_helper(self.head)
 
    def alternate_helper(self, current):
        if current is None:
            return
        print(current.data, end = ' ')
        if current.next:
            self.alternate_helper(current.next.next)
 
a_llist = LinkedList()
data_list = input('Please enter the elements in the linked list: ').split()
for data in data_list:
    a_llist.append(int(data))
 
print('The alternate nodes of the linked list: ', end = '')
a_llist.alternate()
Program Explanation

1. An instance of LinkedList is created.
2. The user is prompted for the elements of the list.
3. The elements are then appended to the linked list.
4. The method alternate is called to display the alternate nodes of the list.

advertisement
advertisement
Runtime Test Cases
Case 1:
Please enter the elements in the linked list: 7 2 1 40 3 10
The alternate nodes of the linked list: 7 1 3 
 
Case 2:
Please enter the elements in the linked list: 4 9 1
The alternate nodes of the linked list: 4 1 
 
Case 3:
Please enter the elements in the linked list: 3
The alternate nodes of the linked list: 3

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!

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.