817. Linked List Components Medium
1/**
2 * [817] Linked List Components
3 *
4 * 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.
5 * Return the number of connected components in nums where two values are connected if they appear consecutively in the linked list.
6 *
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom1.jpg" style="width: 424px; height: 65px;" />
9 * Input: head = [0,1,2,3], nums = [0,1,3]
10 * Output: 2
11 * Explanation: 0 and 1 are connected, so [0, 1] and [3] are the two connected components.
12 *
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom2.jpg" style="width: 544px; height: 65px;" />
15 * Input: head = [0,1,2,3,4], nums = [0,3,1,4]
16 * Output: 2
17 * Explanation: 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.
18 *
19 *
20 * Constraints:
21 *
22 * The number of nodes in the linked list is n.
23 * 1 <= n <= 10^4
24 * 0 <= Node.val < n
25 * All the values Node.val are unique.
26 * 1 <= nums.length <= n
27 * 0 <= nums[i] < n
28 * All the values of nums are unique.
29 *
30 */
31pub struct Solution {}
32use crate::util::linked_list::{ListNode, to_list};
33
34// problem: https://leetcode.com/problems/linked-list-components/
35// discuss: https://leetcode.com/problems/linked-list-components/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39// Definition for singly-linked list.
40// #[derive(PartialEq, Eq, Clone, Debug)]
41// pub struct ListNode {
42// pub val: i32,
43// pub next: Option<Box<ListNode>>
44// }
45//
46// impl ListNode {
47// #[inline]
48// fn new(val: i32) -> Self {
49// ListNode {
50// next: None,
51// val
52// }
53// }
54// }
55impl Solution {
56 pub fn num_components(head: Option<Box<ListNode>>, nums: Vec<i32>) -> i32 {
57 0
58 }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_817() {
69 }
70}
71
Back
© 2025 bowen.ge All Rights Reserved.