Occurrence of an integer in a Linked List | EASY | GFG POTD | 26-10-24 | GFG Problem of the day

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии • 3

  • @codewithuday
    @codewithuday  29 дней назад

    // Using HaspMap or HashTable
    class Solution {
    public:
    int count(struct Node* head, int key) {
    // add your code here
    map map;
    while(head){
    map[head->data]++;
    head = head->next;
    }
    return map[key];
    }
    };

  • @codewithuday
    @codewithuday  29 дней назад

    // Using Set
    class Solution {
    public:
    int count(struct Node* head, int key) {
    // add your code here
    set set;
    int count = 0 ;
    while(head){
    if(set.find(key) != set.end()){
    count++;
    set.erase(key);
    }
    set.insert(head->data);
    head = head->next;
    }
    if(set.find(key) != set.end()){
    count++;
    set.erase(key);
    }
    return count ;

    }
    };

  • @codewithuday
    @codewithuday  29 дней назад

    // Using Counting
    class Solution {
    public:
    int count(struct Node* head, int key) {
    // add your code here
    int count = 0;
    while(head){
    if(head->data == key )count++;
    head = head->next;
    }
    return count;
    }
    };