Remove Every Matching Node from a Linked Chain

Quick Overview

Remove all matching linked nodes with a sentinel predecessor while preserving the remaining chain order.

Remove Every Matching Node from a Linked Chain

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Remove every node whose value equals val from a singly linked list, and return the new head. Keep the remaining nodes in their existing list order. ```cpp ListNode* removeElements(ListNode* head, int val); ``` Use the original ListNode fields val and next. Brackets display chain values, not an array API. ### Examples ```text Input chain: [4,2,4,3,4], val = 4 Output chain: [2,3] ``` ```text Input chain: [5,5], val = 5 Output chain: [] ``` ### Constraints - There are between 0 and 10000 nodes. - Node values lie between 1 and 50. - The removal value lies between 0 and 50.

Overview: Remove all matching linked nodes with a sentinel predecessor while preserving the remaining chain order.

Google logo
Google
Date not reported
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Remove every node whose value equals val from a singly linked list, and return the new head. Keep the remaining nodes in their existing list order.

ListNode* removeElements(ListNode* head, int val);

Use the original ListNode fields val and next. Brackets display chain values, not an array API.

Examples

Input chain: [4,2,4,3,4], val = 4
Output chain: [2,3]
Input chain: [5,5], val = 5
Output chain: []

Constraints

  • There are between 0 and 10000 nodes.
  • Node values lie between 1 and 50.
  • The removal value lies between 0 and 50.