Remove occurrences in Linked List

Easy

Given a linked list and a key, remove all occurrences of the key from the Linked List. Return the head of the resultant list.

Example
Linked List: 1→2→3→2→4→7→2
Key: 2
After removal: 1→3→4→7

Testing

Input Format

The first line contains ‘T’ denoting the number of independent test cases.

For each test case the input has three lines:

  • A number ‘n’, denoting the length of the linked list.
  • n space-separated integers denoting elements of the linked list.
  • A number ‘key’ denoting the key to be deleted.

Output Format

For each test case, the output has one line with space-separated integers denoting the elements of the resultant linked list.

Sample Input

2
7
1 2 3 2 4 7 2
2
3
4 5 6
8

Expected Output

1 3 4 7
4 5 6

Constraints

1 <= T <= 100

0 <= n <= 104

1 <= element <= 1000

1 <= key <= 1000

Editorial Link: Editorial