Count Occurrences in a Linked List

Last Updated : 27 Feb, 2026

Given a singly linked list and a key, the task is to count the number of occurrences of the given key in the linked list.

Example :

Input : head: 1->2->1->2->1->3->1 , key = 1
Output : 4

Count-Occurrences-in-a-Linked-List_1

Explanation: key equals 1 has 4 occurrences.

Input : head: 1->2->1->2->1, key = 3
Output : 0
Explanation: key equals to 3 has 0 occurrences.

Try It Yourself
redirect icon

[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>
using namespace std;

class Node {
public:
    int data;
    Node* next;
  	Node(int data) {
        this->data = data;
        this->next = nullptr;
    }
};

// Counts the no. of occurrences of a key 
// in a linked list
int count(struct Node* head, int key) {
    if (head == NULL)
        return 0;
  	
  	int ans = count(head->next, key);
  
    if (head->data == key)
        ans++;
  
    return ans;
}

int main() {
  
    //Hard Coded Linked List 
  	// 1->2->1->2->1

    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(1);
    head->next->next->next = new Node(2);
    head->next->next->next->next = new Node(1);
	
  	int key = 1;

    cout << count(head, key);
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

// Counts the number of occurrences of a key
// in a linked list using recursion
int count(struct Node* head, int key) {
    if (head == NULL) {
        return 0;
    }
  
  	int ans = count(head->next, key);
  
    if (head->data == key) {
        ans++;
    }
  
  	return ans;
}

struct Node* createNode(int new_data) {
    struct Node* new_node = 
      (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = NULL;
    return new_node;
}

int main() {
  
    // Hard Coded Linked List 
    // 1->2->1->2->1
    struct Node* 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);

    int key = 1;
  
    printf("%d", count(head, key));

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class GfG {

    // Recursive method to count occurrences of a
  	// value in the linked list
    static int count(Node head, int key) {
        if (head == null) {
            return 0;
        }
      	
      	int ans = count(head.next, key);
      
        if (head.data == key) {
            ans++;
        }
        
      	return ans;
    }

    public static void main(String[] args) {
      
        // Hard coded linked list: 
      	// 1 -> 2 -> 1 -> 2 -> 1
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(1);
        head.next.next.next = new Node(2);
        head.next.next.next.next = new Node(1);

        int key = 1;

        System.out.println(count(head, key));
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def count(head, key):
    if head is None:
        return 0
      
    ans = count(head.next, key)
    
    if head.data == key:
        ans += 1
    
    return ans

# 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 = 1

print(count(head, key))
C#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class GfG {
  
    // Recursive method to count occurrences of
  	// a value in the linked list
    static int Count(Node head, int key) {
        if (head == null) {
            return 0;
        }
      	
      	int ans = Count(head.next, key);
      
        if (head.data == key) {
            ans++;
        }
        
      	return ans;
    }

    static void Main(string[] args) {
      
        // Hard coded linked list: 
      	// 1 -> 2 -> 1 -> 2 -> 1
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(1);
        head.next.next.next = new Node(2);
        head.next.next.next.next = new Node(1);

        int key = 1;
        
        Console.WriteLine(Count(head, key));
    }
}
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

// Recursive function to count occurrences of a 
// value in the linked list
function count(head, key) {
    if (head === null) {
        return 0;
    }
    
    let ans = count(head.next, key);
    
    if (head.data === key) {
        ans++;
    }
    
    return ans;
}

// Hard coded linked list:
// 1 -> 2 -> 1 -> 2 -> 1
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(1);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);

let key = 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>
using namespace std;

class Node {
  public:
    int data;
    Node *next;
    Node(int data) {
        this->data = data;
        this->next = nullptr;
    }
};

//Counts the no. of occurrences of a 
// key in a linked list 
int count(Node *head, int key) {
    Node *curr = head;
    int count = 0;
    while (curr != NULL) {
        if (curr->data == key) {
            count++;
        }
        curr = curr->next;
    }
    return count;
}

int main() {
  
    // Hard Coded Linked List
    // 1->2->1->2->1
    Node *head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(1);
    head->next->next->next = new Node(2);
    head->next->next->next->next = new Node(1);

    int key = 1;
    cout << count(head, key);
    return 0;
}
C
#include <stdio.h>

struct Node {
    int data;
    struct Node *next;
};

//Counts the no. of occurrences of a 
// key in a linked list 
int count(struct Node *head, int key) {
    struct Node *curr = head;
    int count = 0;
    while (curr != NULL) {
        if (curr->data == key)
            count++;
        curr = curr->next;
    }
    return count;
}

struct Node *createNode(int new_data) {
    struct Node *new_node = 
      (struct Node *)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = NULL;
    return new_node;
}
int main() {
  
    // Hard Coded Linked List
    // 1->2->1->2->1
    struct Node *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);

    int key = 1;
    printf("%d", count(head, key));
    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class GfG {

    // Method to count occurrences of a value in the linked
    // list
    static int count(Node head, int key) {
        Node curr = head;
        int count = 0;
        while (curr != null) {
            if (curr.data == key) {
                count++;
            }
            curr = curr.next;
        }
        return count;
    }

    public static void main(String[] args) {
      
        // Hard coded linked list: 1 -> 2 -> 1 -> 2 -> 1
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(1);
        head.next.next.next = new Node(2);
        head.next.next.next.next = new Node(1);

        int key = 1;
        System.out.println(count(head, key));
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def count(head, key):
    curr = head
    count = 0
    while curr is not None:
        if curr.data == key:
            count += 1
        curr = curr.next
    return count

# 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 = 1

print(count(head, key))
C#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class GfG {

    // Method to count occurrences of a value
  	// in the linked list
    static int Count(Node head, int key) {
        Node curr = head;
        int count = 0;
        while (curr != null) {
            if (curr.data == key) {
                count++;
            }
            curr = curr.next;
        }
        return count;
    }

    static void Main(string[] args) {
      
        // Hard coded linked list: 
      	// 1 -> 2 -> 1 -> 2 -> 1
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(1);
        head.next.next.next = new Node(2);
        head.next.next.next.next = new Node(1);

        int key = 1;
        Console.WriteLine(Count(head, key));
    }
}
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

// Function to count occurrences of 
// a value in the linked list
function count(head, key) {
    let curr = head;
    let count = 0;
    while (curr !== null) {
        if (curr.data === key) {
            count++;
        }
        curr = curr.next;
    }
    return count;
}

// Hard coded linked list:
// 1 -> 2 -> 1 -> 2 -> 1
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(1);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);

let key = 1;
console.log(count(head, key));

Output
3
Comment