nikunjsoni

817

Mar 16th, 2021
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  *     int val;
  5.  *     ListNode *next;
  6.  *     ListNode() : val(0), next(nullptr) {}
  7.  *     ListNode(int x) : val(x), next(nullptr) {}
  8.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11. class Solution {
  12. public:
  13.     int numComponents(ListNode* head, vector<int>& G) {
  14.         bool vis[100001] = {0};
  15.         for(auto i: G) vis[i] = 1;
  16.         int res = 0;
  17.         while(head){
  18.             if(vis[head->val] && (!head->next || !vis[head->next->val])) res++;
  19.             head = head->next;
  20.         }
  21.         return res;
  22.     }
  23. };
RAW Paste Data