Linked List Components LeetCode solution in C++
1class Solution {2public:3int numComponents(ListNode *head, vector<int> &nums) {4unordered_set<int> s;5for (auto &x : nums) s.insert(x);6int count = 0;7while (head != NULL) {8if (s.find(head->val) != s.end()) count++;9while (head != NULL && s.find(head->val) != s.end()) {10head = head->next;11}12if (head != NULL) head = head->next;13}14return count;15}16};