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;
    }
};

 

posted @ 2020-04-10 09:57  qiujiejie  阅读(88)  评论(0编辑  收藏  举报