Talentd
FleetCode

3217. Delete Nodes From Linked List Present in Array

Medium69.5% acceptance5 companies

You are given an array of integers nums and the head of a linked list. Return the head of the modified linked list after removing all nodes from the linked list that have a value that exists in nums.

Example 1:

Input: nums = [1,2,3], head = [1,2,3,4,5]

Output: [4,5]

Explanation:

Remove the nodes with values 1, 2, and 3.

Example 2:

Input: nums = [1], head = [1,2,1,2,1,2]

Output: [2,2,2]

Explanation:

Remove the nodes with value 1.

Example 3:

Input: nums = [5], head = [1,2,3,4]

Output: [1,2,3,4]

Explanation:

No node has value 5.

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105
  • All elements in nums are unique.
  • The number of nodes in the given list is in the range [1, 105].
  • 1 <= Node.val <= 105
  • The input is generated such that there is at least one node in the linked list that has a value not present in nums.

Hints open one at a time

Hint 1
Add all elements of nums into a Set.
Hint 2
Scan the list to check if the current element should be deleted by checking the Set.
15:19
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1# Definition for singly-linked list.
2# class ListNode:
3# def __init__(self, val=0, next=None):
4# self.val = val
5# self.next = next
6class Solution:
7 def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:
8
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3]
[1,2,3,4,5]
Input
[1]
[1,2,1,2,1,2]
Input
[5]
[1,2,3,4]
Your code is saved on this device.Solution