Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- int numComponents(ListNode* head, vector<int>& G) {
- bool vis[100001] = {0};
- for(auto i: G) vis[i] = 1;
- int res = 0;
- while(head){
- if(vis[head->val] && (!head->next || !vis[head->next->val])) res++;
- head = head->next;
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment