problem no 52:(problem on linked list):find the number of occurances of the given integer inside the linked list.

 QUESTION:

Occurence of an integer in a Linked List
Easy Accuracy: 56.23% Submissions: 31590 Points: 2

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.

Example 2:

Input:
N = 5
Link List = 1->2->1->2->1
search_for = 3
Output: 0
Explanation:3 appears 0 times.
 
ANSWER:
 
int count(struct node* head, int search_for)
{
int count=0;

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

 

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.