Skip to main content
Back to problems
Leetcode
Medium
Linked Lists
Recursion
Two Pointers
Amazon
Remove Linked List Elements

Remove every node whose value matches a given target from a singly linked list.

Acceptance 0%
Problem Statement

Problem

Given the head of a singly linked list and an integer val, remove every node in the list whose value equals val.

Return the head of the modified list.

You should remove all matching nodes and keep the relative order of the remaining nodes.

Notes

  • The list may become empty.
  • Matching nodes can appear at the beginning, middle, or end of the list.
  • You are expected to update pointers in-place rather than rebuild the entire list from scratch.

Input Format

  • head: the head node of a singly linked list
  • val: an integer target value

The linked list node structure typically contains:

clike

Node {
  int value
  Node next
}

Output Format

  • Return the head of the linked list after removing all nodes whose value equals val.

Constraints

  • The linked list may contain zero or more nodes.
  • Node values can repeat.
  • The resulting list may be empty.
  • Aim for linear time and constant extra space, excluding recursion stack if a recursive approach is used.
Examples
Sample cases returned by the problem API.

Example 1

Input

head = [1,2,6,3,4,5,6], val = 6

Output

[1,2,3,4,5]

Explanation

Both nodes with value 6 are removed, including one at the head-side portion and one at the tail.

Example 2

Input

head = [7,7,7,7], val = 7

Output

[]

Explanation

Every node matches the target, so the resulting list is 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.