Linked List Components LeetCode solution in Java
1class Solution {2public int numComponents(ListNode head, int[] nums) {3int count = 0;4HashSet<Integer> set = new HashSet();5for (int i = 0; i < nums.length; i++) {6set.add(nums[i]);7}8while (head != null) {9if (set.contains(head.val)) {10while (head.next != null && set.contains(head.next.val)) {11head = head.next;12}13count++;14}1516head = head.next;17}18return count;19}20}