leetcode 817 Linked List Components

class Solution {
public:
    int numComponents(ListNode* head, vector<int>& G) {
        unordered_set<int> s(G.begin(),G.end());
        ListNode *node=head;
        int re=0;
        while(node) {
            if(s.find(node->val)!=s.end()&&(!node->next||s.find(node->next->val)==s.end())) ++re;
            node=node->next;
        }
        return re;
    }
};
原文地址:https://www.cnblogs.com/LiuQiujie/p/12671489.html