You are preparing for adobe interviews. You came across a problem where you have a sorted linked list of ‘N’ integers. Remove all the occurrences of identical integers from the linked list such that it contains only distinct integers in sorted order.
The first line of input contains an integer ‘T’, the number of test cases.
The first line of each test case contains the linked list separated by space and terminated by -1.
Output Format :
For each test case, print the updated linked list.
Output for each test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 3*10^3
Where ‘N’ is the number of nodes in the linked list.
Time Limit : 1 sec
2
1 3 3 3 4 4 5 -1
1 3 4 4 -1
1 5
1 3
For first test case :
3 and 4 are repeating in the linked list. So, after removing them from the linked list our resultant linked list becomes : 1 5
For second test case :
4 is repeating in the linked list. So, after removing them from the linked list our resultant linked list becomes : 1 3
2
1 3 4 5 -1
1 2 2 -1
1 3 4 5
1
Try to traverse the linked list recursively while checking adjacent numbers.
The basic idea is to traverse the linked list recursively. While traversing, we check the adjacent nodes and remove the nodes if they are equal. Else, we traverse further in the linked list.
Here is the algorithm :
O(N), where ‘N’ is the number of nodes in the linked list.
We traverse the whole linked list recursively to check the adjacent numbers. Therefore, the overall time complexity will be O(N).
O(N), where ‘N’ is the number of nodes in the linked list.
The recursive stack can contain the length of the linked list. Therefore, the overall space complexity will be O(N).