[Naive Approach] By Recursion – O(n) time and O(n) space
The idea is similar to the iterative approach. Here we are using the recursion to check if the node value equals to key or not. Please note that the iterative approach would be better in terms oftime and space. The recursive approach can be good fun exercise or a question in an interview / exam.
Below is the implementation of the above approach :
C++
// C++ program to count occurrences in// a linked list by recursion#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intdata){this->data=data;this->next=nullptr;}};// Counts the no. of occurrences of a key // in a linked listintcount(structNode*head,intkey){if(head==NULL)return0;intans=count(head->next,key);if(head->data==key)ans++;returnans;}intmain(){//Hard Coded Linked List // 1->2->1->2->1Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(1);head->next->next->next=newNode(2);head->next->next->next->next=newNode(1);intkey=1;cout<<count(head,key);return0;}
C
// C program to count occurrences in// a linked list by recursion#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};// Counts the number of occurrences of a key// in a linked list using recursionintcount(structNode*head,intkey){if(head==NULL){return0;}intans=count(head->next,key);if(head->data==key){ans++;}returnans;}structNode*createNode(intnew_data){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=new_data;new_node->next=NULL;returnnew_node;}intmain(){// Hard Coded Linked List // 1->2->1->2->1structNode*head=createNode(1);head->next=createNode(2);head->next->next=createNode(1);head->next->next->next=createNode(2);head->next->next->next->next=createNode(1);intkey=1;printf("%d",count(head,key));return0;}
Java
// Java program to count occurrences in// a linked list by recursionclassNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}publicclassGfG{// Recursive method to count occurrences of a// value in the linked liststaticintcount(Nodehead,intkey){if(head==null){return0;}intans=count(head.next,key);if(head.data==key){ans++;}returnans;}publicstaticvoidmain(String[]args){// Hard coded linked list: // 1 -> 2 -> 1 -> 2 -> 1Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);intkey=1;System.out.println(count(head,key));}}
Python
# Python program to count occurrences in# a linked list by recursionclassNode:def__init__(self,data):self.data=dataself.next=Nonedefcount(head,key):ifheadisNone:return0ans=count(head.next,key)ifhead.data==key:ans+=1returnans# Hard coded linked list: # 1 -> 2 -> 1 -> 2 -> 1head=Node(1)head.next=Node(2)head.next.next=Node(1)head.next.next.next=Node(2)head.next.next.next.next=Node(1)key=1print(count(head,key))
C#
// C# program to count occurrences in// a linked list by recursionusingSystem;classNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGfG{// Recursive method to count occurrences of// a value in the linked liststaticintCount(Nodehead,intkey){if(head==null){return0;}intans=Count(head.next,key);if(head.data==key){ans++;}returnans;}staticvoidMain(string[]args){// Hard coded linked list: // 1 -> 2 -> 1 -> 2 -> 1Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);intkey=1;Console.WriteLine(Count(head,key));}}
JavaScript
// Javascript program to count occurrences in// a linked list by recursionclassNode{constructor(data){this.data=data;this.next=null;}}// Recursive function to count occurrences of a // value in the linked listfunctioncount(head,key){if(head===null){return0;}letans=count(head.next,key);if(head.data===key){ans++;}returnans;}// Hard coded linked list:// 1 -> 2 -> 1 -> 2 -> 1lethead=newNode(1);head.next=newNode(2);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);letkey=1;console.log(count(head,key));
Output
3
[Expected Approach] By Traversing each node – O(n) time and O(1) space
The idea is to traverse through the each node of linked list and check if the node data is equal to the key or not. If it is equal to key we will increment the count.
Below is the implementation of the above approach:
C++
// C++ program to count occurrences in a// linked list#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intdata){this->data=data;this->next=nullptr;}};//Counts the no. of occurrences of a // key in a linked list intcount(Node*head,intkey){Node*curr=head;intcount=0;while(curr!=NULL){if(curr->data==key){count++;}curr=curr->next;}returncount;}intmain(){// Hard Coded Linked List// 1->2->1->2->1Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(1);head->next->next->next=newNode(2);head->next->next->next->next=newNode(1);intkey=1;cout<<count(head,key);return0;}
C
// C program to count occurrences in // a linked list#include<stdio.h>structNode{intdata;structNode*next;};//Counts the no. of occurrences of a // key in a linked list intcount(structNode*head,intkey){structNode*curr=head;intcount=0;while(curr!=NULL){if(curr->data==key)count++;curr=curr->next;}returncount;}structNode*createNode(intnew_data){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=new_data;new_node->next=NULL;returnnew_node;}intmain(){// Hard Coded Linked List// 1->2->1->2->1structNode*head=createNode(1);head->next=createNode(2);head->next->next=createNode(1);head->next->next->next=createNode(2);head->next->next->next->next=createNode(1);intkey=1;printf("%d",count(head,key));return0;}
Java
// C program to count occurrences in// a linked listclassNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}publicclassGfG{// Method to count occurrences of a value in the linked// liststaticintcount(Nodehead,intkey){Nodecurr=head;intcount=0;while(curr!=null){if(curr.data==key){count++;}curr=curr.next;}returncount;}publicstaticvoidmain(String[]args){// Hard coded linked list: 1 -> 2 -> 1 -> 2 -> 1Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);intkey=1;System.out.println(count(head,key));}}
Python
# Python program to count occurrences in# a linked listclassNode:def__init__(self,data):self.data=dataself.next=Nonedefcount(head,key):curr=headcount=0whilecurrisnotNone:ifcurr.data==key:count+=1curr=curr.nextreturncount# Hard coded linked list:# 1 -> 2 -> 1 -> 2 -> 1 head=Node(1)head.next=Node(2)head.next.next=Node(1)head.next.next.next=Node(2)head.next.next.next.next=Node(1)key=1print(count(head,key))
C#
// C# program to count occurrences in// a linked listusingSystem;classNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGfG{// Method to count occurrences of a value// in the linked liststaticintCount(Nodehead,intkey){Nodecurr=head;intcount=0;while(curr!=null){if(curr.data==key){count++;}curr=curr.next;}returncount;}staticvoidMain(string[]args){// Hard coded linked list: // 1 -> 2 -> 1 -> 2 -> 1Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);intkey=1;Console.WriteLine(Count(head,key));}}
JavaScript
// Javascript program to count occurrences in// a linked listclassNode{constructor(data){this.data=data;this.next=null;}}// Function to count occurrences of // a value in the linked listfunctioncount(head,key){letcurr=head;letcount=0;while(curr!==null){if(curr.data===key){count++;}curr=curr.next;}returncount;}// Hard coded linked list:// 1 -> 2 -> 1 -> 2 -> 1lethead=newNode(1);head.next=newNode(2);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);letkey=1;console.log(count(head,key));
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.