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

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: """  
class Node:
    def __init__(self, data):
		self.data = data
		self.next = None
  This is method only submission.
  You only need to complete the method.
"""
class Solution:
    def count(self, head, search_for):
        # Code here

Solutions:
class Solution:

	def count(self, head, search_for):
		if head is None:
			return 0
		cnt = 0
		while head.next:
			if head.data == search_for:
				cnt += 1
			head = head.next
		if head.data == search_for:
			return cnt + 1
		return cnt


class Solution:

	def count(self, head, search_for):
		temp = head
		count = 0
		while temp != None:
			if temp.data == search_for:
				count += 1
			temp = temp.next
		return count


class Solution:

	def count(self, head, search_for):
		c = 0
		ptr = head
		while ptr != None:
			if ptr.data == search_for:
				c += 1
			ptr = ptr.next
		return c


class Solution:

	def count(self, head, search_for):
		current = head
		count = 0
		while current:
			if current.data == search_for:
				count += 1
			current = current.next
		return count


class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

class Solution:

	def count(self, head, search_for):
		count = 0
		curr = head
		while curr is not None:
			if curr.data == search_for:
				count += 1
			curr = curr.next
		return count


class Solution:

	def count(self, head, search_for):
		current = head
		count = 0
		while current is not None:
			if current.data == search_for:
				count = count + 1
			current = current.next
		return count


class Solution:

	def count(self, head, search_for):
		curr_node = head
		counter = 0
		while curr_node:
			if curr_node.data == search_for:
				counter += 1
			curr_node = curr_node.next
		return counter


class Solution:

	def count(self, head, search_for):
		if not head:
			return 0
		else:
			count = 0
			if head.data == search_for:
				count += 1
		count += self.count(head.next, search_for)
		return count


class Solution:

	def count(self, head, search_for):
		l1 = head
		p = 0
		while l1:
			if l1.data == search_for:
				p = p + 1
			l1 = l1.next
		return p


class Solution:

	def count(self, head, search_for):
		d = {}
		while head != None:
			if head.data not in d:
				d[head.data] = 1
			else:
				d[head.data] += 1
			head = head.next
		if search_for in d.keys():
			return d[search_for]
		else:
			return 0


class Solution:

	def count(self, head, search_for):
		c = []
		temp = head
		while temp != None:
			c += [temp.data]
			temp = temp.next
		return c.count(search_for)


class Solution:

	def count(self, head, search_for):
		l = []
		while head:
			l.append(head.data)
			head = head.next
		x = l.count(search_for)
		return x


class Solution:

	def count(self, head, s):
		c = 0
		while head != None:
			if head.data == s:
				c += 1
			head = head.next
		return c


class Solution:

	def count(self, head, search_for):
		current = head
		li = []
		while current is not None:
			li.append(current.data)
			current = current.next
		count = 0
		for i in range(0, len(li)):
			if li[i] == search_for:
				count = count + 1
		return count


class Solution:

	def count(self, head, key):
		c = 0
		while head:
			if head.data == key:
				c += 1
			head = head.next
		return c


class Solution:

	def count(self, head, search_for):
		current = head
		counter = 0
		if head == None:
			return None
		else:
			while current != None:
				if current.data != search_for:
					current = current.next
				elif current.data == search_for:
					counter += 1
					current = current.next
		return counter


class Solution:

	def count(self, head, search_for):
		dict1 = {}
		a = head
		while a is not None:
			if a.data not in dict1:
				dict1[a.data] = 1
			else:
				dict1[a.data] += 1
			a = a.next
		if search_for not in dict1:
			return 0
		else:
			return dict1[search_for]


class Solution:

	def count(self, head, s):
		if head == None:
			return
		ans = 0
		a = head
		while a.next:
			if a.data == s:
				ans += 1
			a = a.next
		if a.data == s:
			ans += 1
		return ans


class Solution:

	def count(self, head, search_for):
		c = 0
		a = head
		while a:
			if a.data == search_for:
				c = c + 1
				a = a.next
			else:
				a = a.next
		return c


class Solution:

	def count(self, head, search_for):
		ans = 0
		if not head:
			return head
		while head.next:
			if head.data == search_for:
				ans += 1
			head = head.next
		if head.data == search_for:
			ans += 1
		return ans


class Solution:

	def count(self, head, search_for):
		count = 0
		if head:
			stack = [head]
			while len(stack) > 0:
				current = stack.pop()
				if current.data == search_for:
					count += 1
				if current.next:
					stack.append(current.next)
			return count
		return count