Given a singly linked list and a key, count the number of occurrences of the given key in the linked list. For example, if the given linked list is 1->2->1->2->1->3->1 and the given key is 1, then the output should be 4.
Method 1- Without Recursion
Algorithm:
Step 1: Start
Step 2: Create A Function Of A Linked List, Pass A Number
As Arguments And Provide The Count Of The Number To The Function.
Step 3: Initialize Count Equal To 0.
Step 4: Traverse In Linked List Until Equal Number Found.
Step 5: If Found A Number Equal To Update Count By 1.
Step 6: After Reaching The End Of The Linked List Return Count.
Step 7: Call The Function.
Step 8: Prints The Number Of Int Occurrences.
Step 9: Stop.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int count(Node* head, int search_for)
{
Node* current = head;
int count = 0;
while (current != NULL) {
if (current->data == search_for)
count++;
current = current->next;
}
return count;
}
int main()
{
Node* head = NULL;
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
cout << "count of 1 is " << count(head, 1);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int count( struct Node* head, int search_for)
{
struct Node* current = head;
int count = 0;
while (current != NULL) {
if (current->data == search_for)
count++;
current = current->next;
}
return count;
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
printf ( "count of 1 is %d" , count(head, 1));
return 0;
}
|
Java
import java.io.*;
class LinkedList {
Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
int count( int search_for)
{
Node current = head;
int count = 0 ;
while (current != null ) {
if (current.data == search_for)
count++;
current = current.next;
}
return count;
}
public static void main(String args[])
{
LinkedList llist = new LinkedList();
llist.push( 1 );
llist.push( 2 );
llist.push( 1 );
llist.push( 3 );
llist.push( 1 );
System.out.println( "Count of 1 is "
+ llist.count( 1 ));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def count( self , search_for):
current = self .head
count = 0
while (current is not None ):
if current.data = = search_for:
count + = 1
current = current. next
return count
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def printList( self ):
temp = self .head
while (temp):
print (temp.data)
temp = temp. next
llist = LinkedList()
llist.push( 1 )
llist.push( 3 )
llist.push( 1 )
llist.push( 2 )
llist.push( 1 )
print ( "count of 1 is % d" % (llist.count( 1 )))
|
C#
using System;
class LinkedList {
Node head;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
int count( int search_for)
{
Node current = head;
int count = 0;
while (current != null ) {
if (current.data == search_for)
count++;
current = current.next;
}
return count;
}
public static void Main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push(1);
llist.push(2);
llist.push(1);
llist.push(3);
llist.push(1);
Console.WriteLine( "Count of 1 is " + llist.count(1));
}
}
|
Javascript
<script>
var head;
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(new_data) {
new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
function count(search_for) {
current = head;
var count = 0;
while (current != null ) {
if (current.data == search_for)
count++;
current = current.next;
}
return count;
}
push(1);
push(2);
push(1);
push(3);
push(1);
document.write( "Count of 1 is " + count(1));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2- With Recursion
Algorithm:
Algorithm
count(head, key);
if head is NULL
return frequency
if(head->data==key)
increase frequency by 1
count(head->next, key)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int frequency = 0;
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int count( struct Node* head, int key)
{
if (head == NULL)
return frequency;
if (head->data == key)
frequency++;
return count(head->next, key);
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
cout << "count of 1 is " << count(head, 1);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
int frequency = 0;
void push(Node** head_ref, int new_data)
{
Node* new_node = (Node*) malloc ( sizeof (Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int count(Node* head, int key)
{
if (head == NULL)
return frequency;
if (head->data == key)
frequency++;
return count(head->next, key);
}
int main()
{
Node* head = NULL;
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
printf ( "count of 1 is %d" , count(head, 1));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Node {
int data;
Node next;
Node( int val)
{
data = val;
next = null ;
}
}
class GFG {
static int frequency = 0 ;
static Node push(Node head, int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
return head;
}
static int count(Node head, int key)
{
if (head == null )
return frequency;
if (head.data == key)
frequency++;
return count(head.next, key);
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 1 );
head = push(head, 3 );
head = push(head, 1 );
head = push(head, 2 );
head = push(head, 1 );
System.out.print( "count of 1 is " + count(head, 1 ));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
self .counter = 0
def count( self , li, key):
if ( not li):
return self .counter
if (li.data = = key):
self .counter = self .counter + 1
return self .count(li. next , key)
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def printList( self ):
temp = self .head
while (temp):
print (temp.data)
temp = temp. next
llist = LinkedList()
llist.push( 1 )
llist.push( 3 )
llist.push( 1 )
llist.push( 2 )
llist.push( 1 )
print ( "count of 1 is" , llist.count(llist.head, 1 ))
|
C#
using System;
public class Node {
public int data;
public Node next;
public Node( int val)
{
data = val;
next = null ;
}
}
class GFG {
static int frequency = 0;
static Node push(Node head, int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
return head;
}
static int count(Node head, int key)
{
if (head == null )
return frequency;
if (head.data == key)
frequency++;
return count(head.next, key);
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 1);
head = push(head, 3);
head = push(head, 1);
head = push(head, 2);
head = push(head, 1);
Console.Write( "count of 1 is " + count(head, 1));
}
}
|
Javascript
<script>
class Node
{
constructor(val)
{
this .data = val;
this .next = null ;
}
}
let frequency = 0;
function push(head, new_data)
{
let new_node = new Node(new_data);
new_node.next = head;
head = new_node;
return head;
}
function count(head, key)
{
if (head == null )
return frequency;
if (head.data == key)
frequency++;
return count(head.next, key);
}
let head = null ;
head = push(head, 1);
head = push(head, 3);
head = push(head, 1);
head = push(head, 2);
head = push(head, 1);
document.write( "count of 1 is " +
count(head, 1));
</script>
|
Time complexity: O(n) where n is size of linked list
Auxiliary Space: O(n) for call stack since using recursion
Below method can be used to avoid Global variable ‘frequency'(counter in case of Python 3 Code).
C++
int count( struct Node* head, int key)
{
if (head == NULL)
return 0;
if (head->data == key)
return 1 + count(head->next, key);
return count(head->next, key);
}
|
Java
import java.io.*;
int count(Node head, int key)
{
if (head == null )
return 0 ;
if (head.data == key)
return 1 + count(head.next, key);
return count(head.next, key);
}
|
C#
using System;
static int count(Node head, int key)
{
if (head == null )
return 0;
if (head.data == key)
return 1 + count(head.next, key);
return count(head.next, key);
}
|
Python3
def count( self , temp, key):
if temp is None :
return 0
if temp.data = = key:
return 1 + count(temp. next , key)
return count(temp. next , key)
|
Javascript
<script>
function count(head , key)
{
if (head == null )
return 0;
if (head.data == key)
return 1 + count(head.next, key);
return count(head.next, key);
}
</script>
|
The above method implements head recursion. Below given is the tail recursive implementation for the same. Thanks to Puneet Jain for suggesting this approach:
int count(struct Node* head, int key)
{
if(head == NULL)
return 0;
int frequency = count(head->next, key);
if(head->data == key)
return 1 + frequency;
// else
return frequency;
}
Time Complexity : O(n)
Auxiliary Space : O(n)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Mar, 2023
Like Article
Save Article