Occurence of an integer in a Linked List


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



Example 1:


Input:

N = 7

Link List = 1->2->1->2->1->3->1

search_for = 1

Output: 4

Explanation:1 appears 4 times.

int count(struct node* head, int x)
{
    int c=0;
    while(head!=NULL)
    {
         if(head->data==x)
         c++;
         head=head->next;
    }
    return c;
Time Complexity: O(n)  


No comments

darkmode