P21710 [GeeksforGeeks] 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.
Example 2:
Input:
N = 5
Link List = 1->2->1->2->1
search_for = 3
Output: 0
Explanation:3 appears 0 times.
Your Task:
You dont need to read input or print anything. Complete the function count() which takes the head of the link list and search_for i.e- the key as input parameters and returns the count of occurrences of the given key.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ N ≤ 10^4

输入格式

见题面

输出格式

见题面

样例输入
N = 7 Link List = 1->2->1->2->1->3->1 search_for = 1
样例输出
4