Remove Linked List Elements

EasyLinked Listtwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given the head of a singly linked list and an integer val. Delete every node whose value equals val and return the head of the resulting list. The relative order of the surviving nodes must not change. If every node is removed, or the list was empty to begin with, return null (an empty list).

Examples

Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]
Both nodes holding 6 are unlinked.
Input: head = [7,7,7,7], val = 7
Output: []
Every node matches, so nothing is left.
Input: head = [], val = 1
Output: []
An empty list stays empty.

Constraints

  • 0 <= number of nodes <= 10^4
  • 1 <= Node.val <= 50
  • 0 <= val <= 50

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems