Check out example codes for "Describe a recursive algorithm that counts the number of nodes in a singly linked list.". It will help you in understanding the concepts better.

Code Example 1

``````# A complete working Python program to find length of a

# Node class
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data = data  # Assign data
self.next = None  # Initialize next as null

# Linked List class contains a Node object

def __init__(self):

# This function is in LinkedList class. It inserts
# a new node at the beginning of Linked List.
def push(self, new_data):

# 1 & 2: Allocate the Node &
#        Put in the data
new_node = Node(new_data)

# 3. Make next of new Node as head

# 4. Move the head to point to new Node

# This function counts number of nodes in Linked List
# recursively, given 'node' as starting node.
def getCountRec(self, node):
if (not node): # Base case
return 0
else:
return 1 + self.getCountRec(node.next)

# A wrapper over getCountRec()
def getCount(self):