FastPrepRemove Linked List Elements

Remove Linked List Elements

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

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

Return the head of the remaining list. Reuse the nodes that are not removed.

Function

removeElements(head: ListNode, val: int) → ListNode

Examples

Example 1

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

Both nodes containing 6 are removed.

Example 2

head = []val = 1return = []

An empty list stays empty.

Example 3

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

Removing every node yields an empty list.

Constraints

  • The list contains between 0 and 100000 nodes.
  • -1000000000 <= Node.val, val <= 1000000000.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public ListNode removeElements(ListNode head, int val) {
    // Write your code here.
}
head[1,2,6,3,4,5,6]
val6
expected[1,2,3,4,5]
Checking account…