New update is available. Click here to update.

Topics

You are given a linked list of N nodes. Your task is to remove the duplicate nodes from the linked list such that every element in the linked list occurs only once i.e. in case an element occurs more than once, only keep its first occurrence in the list.

```
Assuming the linked list is 3 -> 2 -> 3 -> 4 -> 2 -> 3 -> NULL.
Number ‘2’ and ‘3’ occurs more than once. Hence we remove the duplicates and keep only their first occurrence. So, our list becomes : 3 -> 2 -> 4 -> NULL.
```

Detailed explanation

```
1 <= T <= 100
1 <= N <= 10 ^ 4
1 <= data <= 10 ^ 5
Where ‘N’ is the number of nodes in the list and 'data' is the value of list nodes.
Time Limit: 1sec
```

```
2
4 2 5 4 2 2 -1
1 2 1 2 2 2 7 7 -1
```

```
4 2 5 -1
1 2 7 -1
```

```
For the first test case, the linked list is 4 -> 2 -> 5 -> 4 -> 2 -> 2 -> NULL. Number ‘4’ and ‘2’ occurs more than once. Hence, we remove the duplicates and keep only their first occurrence. So, our list becomes : 4 -> 2 -> 5 -> NULL.
For the second test case, the linked list is 1 -> 2 -> 1 -> 2 -> 2 -> 2 -> 7 -> 7 -> NULL. Number ‘1’, ‘2’ and ‘7’ occurs more than once. Hence, we remove the duplicates and keep only their first occurrence. So, our list becomes : 1 -> 2 -> 7 -> NULL.
```

```
2
3 3 3 3 3 -1
10 20 10 20 30 10 20 30 -1
```

```
3 -1
10 20 30 -1
```