Class Solution

java.lang.Object
g0801_0900.s0817_linked_list_components.Solution

public class Solution extends Object
817 - Linked List Components.<p>Medium</p> <p>You are given the <code>head</code> of a linked list containing unique integer values and an integer array <code>nums</code> that is a subset of the linked list values.</p> <p>Return <em>the number of connected components in</em> <code>nums</code> <em>where two values are connected if they appear <strong>consecutively</strong> in the linked list</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom1.jpg" alt="" /></p> <p><strong>Input:</strong> head = [0,1,2,3], nums = [0,1,3]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> 0 and 1 are connected, so [0, 1] and [3] are the two connected components.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom2.jpg" alt="" /></p> <p><strong>Input:</strong> head = [0,1,2,3,4], nums = [0,3,1,4]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the linked list is <code>n</code>.</li> <li><code>1 <= n <= 10<sup>4</sup></code></li> <li><code>0 <= Node.val < n</code></li> <li>All the values <code>Node.val</code> are <strong>unique</strong>.</li> <li><code>1 <= nums.length <= n</code></li> <li><code>0 <= nums[i] < n</code></li> <li>All the values of <code>nums</code> are <strong>unique</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numComponents

      public int numComponents(ListNode head, int[] nums)