Linked List Components LeetCode solution in C++

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0