Python Program to Count the Occurrences of Elements in a Linked List without Recursion

This is a Python program to count the number of occurrences of an element in a linked list without using recursion.

Problem Description

The program prompts the user for a key and displays the number of times the key occurs in the linked list.

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 method count.
5. count uses a loop and a variable to count the number of occurrences of a key.
6. Create an instance of LinkedList, append data to it and display the list.
7. Prompt the user for a key and display the number of times it occurs by calling the method count.

Program/Source Code

Here is the source code of a Python program to count the number of occurrences of an element in a linked list without 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):
        current = self.head
        while current:
            print(current.data, end = ' ')
            current = current.next
 
    def count(self, key):
        current = self.head
 
        count = 0
        while current:
            if current.data == key:
                count = count + 1
            current = current.next
 
        return count
 
a_llist = LinkedList()
for data in [5, 1, 3, 5, 5, 15, 4, 9, 2]:
    a_llist.append(data)
print('The linked list: ', end = '')
a_llist.display()
print()
 
key = int(input('Enter data item: '))
count = a_llist.count(key)
print('{0} occurs {1} time(s) in the list.'.format(key, count))
Program Explanation

1. An instance of LinkedList is created.
2. Some elements are appended to the list.
3. The linked list is displayed.
4. The user is prompted for a key to enter.
5. count counts the number of times the key occurs in the list.
6. The count determined is then displayed.

advertisement
Runtime Test Cases
Case 1:
The linked list: 5 1 3 5 5 15 4 9 2 
Enter data item: 5
5 occurs 3 time(s) in the list.
 
Case 2:
The linked list: 5 1 3 5 5 15 4 9 2 
Enter data item: 3
3 occurs 1 time(s) in the list.
 
Case 3:
The linked list: 5 1 3 5 5 15 4 9 2 
Enter data item: 7
7 occurs 0 time(s) in the list.

Sanfoundry Global Education & Learning Series – 1000 Python Programs.

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

🎓 Register Today for Free C++ Certification - December 2025

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