Linked List Components
Every value in the list is different. nums holds some of those values. A component is a maximal stretch of consecutive nodes whose values are all in nums. Return the number of components. The list is passed in as head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
head [0,1,2,3]
nums [0,1,3]
returns 2
0,1 form one component and 3 forms another.
Example 2
head [0,1,2,3,4]
nums [0,3,1,4]
returns 2
0,1 and 3,4.
Constraints
1 <= number of nodes <= 50- All node values are different,
0 <= Node.val <= 100 - Every number in
numsis a node value
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.