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.
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.
You have to count a number of occurrences of given key in linked list and return the count value.
The task is to complete the function count() which counts the number of occurrences of given element.
1 2 2 4 5 6 7 8
Testcase 1: 2 is occurring twice in the given linked list.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.