Skip to main content
Back to problems
Leetcode
Medium
Linked Lists
Hash Maps
Arrays
Delete Nodes From Linked List Present In Array

Remove every node from a singly linked list whose value appears in a given array.

Acceptance 0%
Problem Statement

Problem

You are given the head of a singly linked list and an array of integers. Delete every node from the linked list whose value is present in the array.

Return the head of the modified linked list.

The relative order of the remaining nodes should stay the same.

Notes

  • A node should be removed if its value matches any value in the array.
  • The linked list may become empty after all deletions.
  • The array may contain duplicate values; treat it as a set of values to remove.

Input Format

  • A linked list represented by its head node.
  • An integer array containing values to delete.

Output Format

  • The head node of the linked list after removing all matching nodes.

Constraints

  • The linked list contains at least 1 node unless all nodes are deleted.
  • Values can be any integers.
  • Aim for a solution that runs in linear time relative to the list length plus the array length.

Hints

  • Convert the array into a structure that supports fast membership checks.
  • Use a dummy head to simplify deletions near the front of the list.

Input Format

  • head: head of a singly linked list
  • nums: integer array of values to delete

Output Format

  • Head of the linked list after removing all nodes whose values appear in nums

Constraints

  • Preserve the order of remaining nodes
  • Duplicate values in nums should not change the result
  • Prefer an O(n+m)O(n + m) approach, where nn is the list length and mm is the array length
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

[1,3,5]

Explanation

Nodes with values 2 and 4 are removed.

Example 2

Input

head = [7,7,7], nums = [7]

Output

[]

Explanation

Every node matches a value to delete, so the list becomes empty.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.