Occurence of an integer in a Linked List

PROBLEM :

Given a singly linked list and a key, count number of occurrences of given key in linked list. For example, if given linked list is 1->2->1->2->1->3->1 and given key is 1, then output should be 4.

Input:
You have to complete the method which takes two argument: the head of the linked list and int k. You should not read any input from stdin/console.
The struct Node has a data part which stores the data and a next pointer which points to the next element of the linked list.
There are multiple test cases. For each test case, this method will be called individually.

Output:
You have to count a number of occurrences of given key in linked list and return the count value.

Note: If you use "Test" or "Expected Output Button" use below example format

Example:
Input:
1
8
1 2 2 4 5 6 7 8
2

Output:
2

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*
  Return the no. of times search_for value is there in a linked list.
  The input list will have at least one element

  Node is defined as
  struct node
  {
     int data;
     struct node *next;
  }
*/

int count(struct node* head, int search_for)
{
    if(head==NULL)
        return 0;
       
    int count=0 ;
    while(head!=NULL)
    {
        if(head->data==search_for)
            count++ ;
        head=head->next ;
    }
    return count ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )