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 Print the Alternate Nodes in a Linked List using Recursion
When working with linked lists, we often need to print alternate nodes (1st, 3rd, 5th, etc.). This can be achieved using recursion by skipping one node at each recursive call. We'll create a linked list class with methods to add elements and a recursive function to print alternate nodes.
Node Class
First, let's define a simple Node class to represent individual elements in our linked list ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
Linked List Implementation
Next, we'll create a linked list class with methods to add elements and print alternate nodes recursively ?
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 print_all(self):
current = self.head
while current:
print(current.data, end=' ')
current = current.next
print()
def print_alternate_nodes(self):
print("Alternate nodes: ", end='')
self.alternate_helper(self.head)
print()
def alternate_helper(self, current):
if current is None:
return
print(current.data, end=' ')
if current.next:
self.alternate_helper(current.next.next)
# Create linked list and add elements
linked_list = LinkedList()
elements = [78, 56, 34, 52, 71, 96, 0, 80]
for elem in elements:
linked_list.add_value(elem)
print("All elements:", end=' ')
linked_list.print_all()
linked_list.print_alternate_nodes()
All elements: 78 56 34 52 71 96 0 80 Alternate nodes: 78 34 71 0
How the Recursion Works
The recursive function alternate_helper() works by ?
- Base Case: If current node is None, return (end recursion)
- Print: Print the current node's data
-
Skip: Skip the next node by calling recursively with
current.next.next
Step-by-Step Execution
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 alternate_helper(self, current, position=1):
if current is None:
return
print(f"Position {position}: {current.data}")
if current.next:
# Skip next node, go to position+2
self.alternate_helper(current.next.next, position + 2)
def print_alternate_with_position(self):
print("Printing alternate nodes with positions:")
self.alternate_helper(self.head)
# Example with position tracking
linked_list = LinkedList()
for i in [10, 20, 30, 40, 50]:
linked_list.add_value(i)
linked_list.print_alternate_with_position()
Printing alternate nodes with positions: Position 1: 10 Position 3: 30 Position 5: 50
Key Points
-
Recursion: The function calls itself with
current.next.nextto skip one node -
Base Case: When
currentis None, recursion stops - Time Complexity: O(n/2) where n is the number of nodes
- Space Complexity: O(n/2) due to recursive call stack
Conclusion
Using recursion to print alternate nodes in a linked list provides an elegant solution. The key is to skip one node at each recursive call by using current.next.next, which naturally prints every alternate element starting from the head.
