{"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-&gt;2-&gt;1-&gt;2-&gt;1-&gt;3-&gt;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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to count the number of time a given<br\/># int occurs in a linked list<br\/> <br\/># Node class <br\/>class Node:<br\/> <br\/>    # Constructor to initialize the node object<br\/>    def __init__(self, data):<br\/>        self.data = data<br\/>        self.next = None<br\/> <br\/>class LinkedList:<br\/> <br\/>    # Function to initialize head<br\/>    def __init__(self):<br\/>        self.head = None<br\/> <br\/>    # Counts the no . of occurances of a node<br\/>    # (seach_for) in a linkded list (head)<br\/>    def count(self, search_for):<br\/>        current = self.head<br\/>        count = 0<br\/>        while(current is not None):<br\/>            if current.data == search_for:<br\/>                count += 1<br\/>            current = current.next<br\/>        return count<br\/> <br\/>    # Function to insert a new node at the beginning<br\/>    def push(self, new_data):<br\/>        new_node = Node(new_data)<br\/>        new_node.next = self.head<br\/>        self.head = new_node<br\/> <br\/>    # Utility function to print the linked LinkedList<br\/>    def printList(self):<br\/>        temp = self.head<br\/>        while(temp):<br\/>            print temp.data,<br\/>            temp = temp.next<br\/> <br\/> <br\/># Driver program<br\/>llist = LinkedList()<br\/>llist.push(1)<br\/>llist.push(3)<br\/>llist.push(1)<br\/>llist.push(2)<br\/>llist.push(1)<br\/> <br\/># Check for the count function<br\/>print &quot;count of 1 is %d&quot; %(llist.count(1))<br\/> <br\/># This code is contributed by Nikhil Kumar Singh(nickzuck_007)<\/code><\/pre> <\/div>\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=&#8221;banner&#8221;]\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}]}}