{"id":27084,"date":"2018-01-02T22:09:58","date_gmt":"2018-01-02T16:39:58","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27084"},"modified":"2018-01-02T22:09:58","modified_gmt":"2018-01-02T16:39:58","slug":"write-function-counts-number-times-given-int-occurs-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/write-function-counts-number-times-given-int-occurs-linked-list\/","title":{"rendered":"Python Algorithm &#8211; Write a function that counts the number of times a given int occurs in a Linked List"},"content":{"rendered":"<p>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.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre>1. Initialize count as zero.\r\n2. Loop through each element of linked list:\r\n     a) If element data is equal to the passed number then\r\n        increment the count.\r\n3. Return count. \r\n<\/pre>\n<p><strong>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20count%20the%20number%20of%20time%20a%20given%0A%23%20int%20occurs%20in%20a%20linked%20list%0A%20%0A%23%20Node%20class%20%0Aclass%20Node%3A%0A%20%0A%20%20%20%20%23%20Constructor%20to%20initialize%20the%20node%20object%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%0A%20%0Aclass%20LinkedList%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialize%20head%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%20%20%20%23%20Counts%20the%20no%20.%20of%20occurances%20of%20a%20node%0A%20%20%20%20%23%20(seach_for)%20in%20a%20linkded%20list%20(head)%0A%20%20%20%20def%20count(self%2C%20search_for)%3A%0A%20%20%20%20%20%20%20%20current%20%3D%20self.head%0A%20%20%20%20%20%20%20%20count%20%3D%200%0A%20%20%20%20%20%20%20%20while(current%20is%20not%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20current.data%20%3D%3D%20search_for%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.next%0A%20%20%20%20%20%20%20%20return%20count%0A%20%0A%20%20%20%20%23%20Function%20to%20insert%20a%20new%20node%20at%20the%20beginning%0A%20%20%20%20def%20push(self%2C%20new_data)%3A%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20self.head%0A%20%20%20%20%20%20%20%20self.head%20%3D%20new_node%0A%20%0A%20%20%20%20%23%20Utility%20function%20to%20print%20the%20linked%20LinkedList%0A%20%20%20%20def%20printList(self)%3A%0A%20%20%20%20%20%20%20%20temp%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while(temp)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20temp.data%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%0A%20%0A%20%0A%23%20Driver%20program%0Allist%20%3D%20LinkedList()%0Allist.push(1)%0Allist.push(3)%0Allist.push(1)%0Allist.push(2)%0Allist.push(1)%0A%20%0A%23%20Check%20for%20the%20count%20function%0Aprint%20%22count%20of%201%20is%20%25d%22%20%25(llist.count(1))%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Nikhil%20Kumar%20Singh(nickzuck_007)%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>count of 1 is 3<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n)<br \/>\n<strong>Auxiliary Space:<\/strong> O(1)<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Python Algorithm &#8211; Write a function that counts the number of times a given int occurs in a Linked List &#8211; Linked List &#8211; Given a singly linked list and a key<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[80781,80785,80783,80788,80240,80782,80786,80787,80790,80789,80784],"class_list":["post-27084","post","type-post","status-publish","format-standard","hentry","category-linked-list","category-singly-linked-list","tag-algorithm-to-count-the-number-of-nodes-in-linked-list","tag-c-program-to-count-repeated-number-in-an-array","tag-count-nodes-in-linked-list-c","tag-count-the-number-of-nodes-in-a-doubly-linked-list","tag-find-length-of-linked-list-java","tag-how-to-count-the-number-of-nodes-in-a-linked-list-java","tag-linked-list-count-java","tag-pseudo-code-to-delete-a-singly-linked-list","tag-pseudocode-to-delete-a-singly-linked-list","tag-write-a-program-to-count-the-number-of-times-an-item-is-present-in-a-linked-list","tag-write-a-python-program-that-accept-a-word-from-the-user-and-reverse-it"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27084"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27084\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}