Write a function that counts the number of times a given int occurs in a Linked List

Here is a solution.


1. Initialize count as zero.
2. Loop through each element of linked list:
     a) If element data is equal to the passed number then
        increment the count.
3. Return count. 



/* Link list node */
struct node 
    int data;
    struct node* next;

/* Given a reference (pointer to pointer) to the head
  of a list and an int, push a new node on the front 
  of the list. */
void push(struct node** head_ref, int new_data) 
    /* allocate node */
    struct node* new_node = 
            (struct node*) malloc(sizeof(struct node)); 

    /* put in the data  */
    new_node->data  = new_data; 
    /* link the old list off the new node */
    new_node->next = (*head_ref); 
    /* move the head to point to the new node */
    (*head_ref)    = new_node; 

/* Counts the no. of occurences of a node 
   (search_for) in a linked list (head)*/
int count(struct node* head, int search_for) 
    struct node* current = head;
    int count = 0;
    while (current != NULL) 
        if (current->data == search_for)
        current = current->next;
    return count;

/* Drier program to test count function*/
int main()
    /* Start with the empty list */
    struct node* head = NULL;
    /* Use push() to construct below list
     1->2->1->3->1  */
    push(&head, 1); 
    push(&head, 3); 
    push(&head, 1);  
    push(&head, 2);
    push(&head, 1);    
    /* Check the count function */
    printf("count of 1 is %d", count(head, 1));    

Time Complexity: O(n)
Auxiliary Space: O(1)

Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.

  • deepuanand

    Via Tail Recursion…

    int count_n_in_ll(node_t *head,int n)
    static int count = 0;
    if(head == NULL) {
    if(count == 0) {
    printf(“either element not present in list or linklist is emptyn”);
    return -1;
    return count;
    if(head->data == n)
    return count_n_in_ll(head->next,n);

  • Sandeep

    public void countRepeated(int n){

    Node main = start;

    int count = 0;

    if(main.getData() == n){ //To check for start node

    while(main.getLink() != null){ //To check for remaining nodes excluding //last node

    if(main.getData() == n){

    main = main.getLink();

    if(main.getData() == n){ //To check for last node

    System.out.println(“The count of repeated number is : ” + count);


  • ravikant

    Common people post questions like these ๐Ÿ˜›
    They spoil such a good site !!

    • http://www.hate.com student

      what do you mean y common people? Are u a super hero or master of disasters something ? It is because of people like you that good is getting better and bad is getting worst

    • neha2210

      Common people learn and become good programmers. I believe you think you were never a common person!

    • abhishek08aug

      Wow! This is the most uncommon/retarded comment I came across ever on this site. ๐Ÿ˜€

    • Sudarshan Singh

      Cool..man ..I have also astonished on this post but its ok…even a single person needs it its ok

    • a.rookie.programmer

      this site is for common people.. if u think u are an exceptional programmer either go find a better site or make ur own.. btw thanks gfg for posting this..

  • geeksforgeeks

    @Snehal: Time complexity is definitely O(n) but space complexity is O(1) as we are using constant extra space.

    • Prateek Sharma

      I think Auxiliary space is o(1) but space complexity is o(n)…

      /* Paste your code here (You may delete these lines if not writing code) */
      • GeeksforGeeks

        Thanks for pointing this out. We have updated the post.

    • Shailedra

      i think Space complexity singly linked list is O(n)

  • Snehal

    I didnt get how it is O(1)?
    anyway we need to traverse the complete linked list to count the occurrence of the element ?if you are assuming
    n==(constant) and so it is o(1) , then it is wrong assumption,becoz at worst/base/avg case u need to move till end of the ll in the approach used by u

  • geeksforgeeks

    @Shikha: Thanks very much for pointing this out. We have corrected the space complexity.

  • http://geeksforgeeks.org/?p=852 Shikha

    Space complexity is O(1) not O(n) here. ( http://geeksforgeeks.org/?p=852 )