Skills101
← All coding problems

Remove Linked List Elements

easyLinked List & Stack II+10 XPTime limit 2s

A playlist is stored as a linked list of song ids. The listener blocks song x, so every node with value x must be removed. Print the playlist that is left.

List format. A linked list is given as two lines: its length n, then its n values from head to tail (the second line is empty when n = 0). When you print a list, print its values on one line separated by spaces, or the word empty if it has no nodes.

Input: one list, then a third line x.

Output: the list after removing every x (empty if nothing is left).

Constraints: 0 ≤ n ≤ 10⁵, −50 ≤ value, x ≤ 50.

Think about it: the head itself may have to go — maybe several heads in a row. A dummy node in front of the head turns that into the normal case: look at cur.next, and either unlink it or step forward.

Sample input 1

7
1 2 6 3 4 5 6
6

Sample output 1

1 2 3 4 5

Sample input 2

4
7 7 7 7
7

Sample output 2

empty

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.