Guest User

Untitled

a guest
Apr 26th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. int numComponents(ListNode* head, vector<int>& G) {
  12. // save g in set
  13. unordered_set<int> s(G.begin(), G.end());
  14. int result = 0;
  15.  
  16. if(!head)
  17. return result;
  18.  
  19. auto p = head;
  20. auto q = head->next;
  21.  
  22. while(q){
  23. if(s.find(q->val) != s.end() && s.find(p->val) != s.end()){
  24. result += 1;
  25. cout<<q->val<<" "<<p->val<<endl;
  26. }
  27. p = q;
  28. q = q->next;
  29. }
  30.  
  31. return G.size() - result;
  32. }
  33. };
Add Comment
Please, Sign In to add comment