FunctionRacerProblemsRace

Linked List Components

EasyLinked listsPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Linked lists problems