Problem 281455 · easy · Phase 02 Linear Data Structures

Delete Every Occurrence

linked list · deletion · dummy node

Given the head of a singly linked list and a value val, remove every node whose value equals val and return the head of the resulting list.

Examples

Input:  head = 1 -> 2 -> 2 -> 3 -> 2, val = 2
Output: 1 -> 3

Input:  head = 2 -> 2, val = 2
Output: (empty)

Constraints

  • 0 <= number of nodes <= 10**4
  • Target: O(n) time, O(1) extra space, one pass

Goals

  • Remove several nodes in a single pass
  • Avoid advancing the predecessor after a deletion
Starting Python…