[Naive Approach] By Recursion – O(n) time and O(n) space
The idea is to recursively traverse the linked list from the head. At each node, check if its value matches the given key; if it does, increment the count by 1. Then recursively call the function on the next node. When the pointer reaches NULL, return 0. The total count is obtained by summing the matches from all recursive calls.
C++
#include<iostream>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
#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
classNode{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));}}
usingSystem;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
classNode{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 the linked list iteratively from the head, maintaining a counter. For each node, check if its value matches the key; if it does, increment the counter. Continue until the end of the list and return the final count.
C++
#include<iostream>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
#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
classNode{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));}}
usingSystem;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
classNode{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));