All problems

Problem 115

Remove Linked List Elements

Easy · 10 pts · Linked List, Recursion

Problem

You are given a linked list and a value V. Remove every node whose value equals V and print the list that remains. The removed nodes can be anywhere, including the head, and the whole list may disappear, in which case you print EMPTY. A dummy node placed before the head lets you treat the head like every other node: walk through the list and, whenever the next node holds V, skip over it by linking around it.

Input

First line: integers N and V. Second line: the N node values (empty if N = 0).

Output

Print the node values of the resulting list from head to tail, space-separated, or EMPTY if it has no nodes.

Constraints

0 <= N <= 10000; 1 <= value, V <= 50

Example input

7 6
1 2 6 3 4 5 6

Example output

1 2 3 4 5

Explanation

In 1 2 6 3 4 5 6 with V = 6, the two nodes holding 6 are removed. The remaining nodes in order are 1 2 3 4 5.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Merge Two Sorted Lists Next: Partition List