Skip to content

Practice Linked List Components

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#817
LeetCode Problem

Linked List Components

Problem Statement

You are given the head of a linked list containing unique integer values and an integer array nums that is a subset of the linked list values. Return the number of connected components in nums where two values are connected if they appear consecutively in the linked list.

Source: Linked List Components on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
head = [0,1,2,3], nums = [0,1,3]
Output
2

# Example 2

Input
head = [0,1,2,3,4], nums = [0,3,1,4]
Output
2

Constraints

The number of nodes in the linked list is n.
1 <= n <= 10^4
0 <= Node.val < n
All the values Node.val are unique.
1 <= nums.length <= n
0 <= nums[i] < n
All the values of nums are unique.

Related LeetCode Problems

Practice more problems similar to Linked List Components.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.