Occurence of an integer in a Linked List | Practice | GeeksforGeeks

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.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ N ≤ 10^4
0
alokkumar90191 week ago
int count(struct node* head, int x)
   {
   //add your code here
   int count=0;
   node *curr=head;
   if(head==NULL){
       return count;
   }
   while(curr!=NULL){
       if(curr->data==x){
           count++;
       }
       curr=curr->next;
   }
   return count;
   }
0
tthakare732 weeks ago
//java solution
class Solution{
    public static int count(Node head, int search_for){
        //code here
        int count = 0;
        while(head != null){
            if(head.data  == search_for) count++;
            head = head.next;
        }
        return count;
    }
}
0
09himanshusah2 weeks ago
#JavaScript 
class Solution
{
    count(head, search_for)
    {
        //add your code here
        let cur = head;
        let count = 0;
        while(cur) {
            if(cur.data === search_for) count++;
            cur = cur.next;
        }
        return count;
    }
}
0
mankesh0162 weeks ago
    node* temp=head;
        int occ=0;
        while(temp){
            if(temp->data == search_for) occ++;
            temp=temp->next;
        }
        return occ;
0
amiransarimy4 weeks ago
Python Solutions
 
def count(self, head, search_for):
       count = 0
       curr = head
       
       while curr:
           if curr.data ==search_for:
               count += 1
           curr = curr.next
       return count
0
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (0.3/1.3 SEC)
class Solution{   public static int count(Node head, int search_for)   {       int count=0;       Node temp=head;       while(temp!=null)       {           if(temp.data==search_for)           {               count++;           }           temp=temp.next;       }       return count;   }}
0
emmanueluluabuike2 months ago
Simple Java solution
 public static int count(Node head, int search){
        //code here
        Node curr = head;
        int count = 0;
        
        if(curr == null)
            return count;
            
        while(curr != null){
            if(curr.data == search)
                count++;
            curr = curr.next;
        }    
            
        return count;
    }
0
pandeyprakash9672 months ago
node* temp=head;       int count=0;       while(temp!=NULL)       {           if(temp->data==search_for)           {               count++;           }           temp=temp->next;       }       return count;
0
nagendiranooty2 months ago
in javaScript
 
class Solution{   count(head, search_for)   {       //add your code here       var count = 0;       var a = new Array(0);       var i = 0;       while(head !== null)       {           if(head.data == search_for)           {               count++;           }           a[i] = head.data;           head = head.next;           i++;       }              var b = 0;       var c = 0;       var counts = 0;       var g = 0;       while(b < a.length)       {           while(c < a.length)           {               counts = 0;               if(a[b] == a[c])               {                   counts++;                   if(counts === search_for)                   {                       g = a[c];                   }               }               c++;           }           b++;       }                     if(count != search_for )       {           return count;       }              if(g === search_for)       {           return g;       }              if(count === search_for && g != search_for)
       {           return count;       }   }}
+1
codeman992 months ago
class Solution
{
   public:
   int count(struct node* head, int search_for){
   //add your code here
       int ans=0;
       node* temp=head;
       while(temp!=NULL){
           if(temp->data==search_for){
               ans++;
           }
           temp=temp->next;
       }
       return ans;
   }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.