Problem of the day
A doubly-linked list is a data structure that consists of sequentially linked nodes, and the nodes have reference to both the previous and the next nodes in the sequence of nodes.
You are given a sorted doubly linked list of size 'n'.
Remove all the duplicate nodes present in the linked list.
Input: Linked List: 1 <-> 2 <-> 2 <-> 2 <-> 3
Output: Modified Linked List: 1 <-> 2 <-> 3
Explanation: We will delete the duplicate values ‘2’ present in the linked list.
The first line contains an integer 'n', the number of elements in the linked list.
The second line contains 'n' integers, the elements of the linked list separated by a single space.
Print a single line, the final linked list.
You are not required to print anything; it has already been taken care of. Just implement the function.
5
1 2 2 2 3
1 2 3
We will delete the duplicate values ‘2’ present in the linked list.
4
1 2 3 4
1 2 3 4
The list contains no duplicates, so the final list is unchanged.
The expected time complexity is O('n').
1 <= 'n' <= 10^5
1 <= 'data' in any node <= 10^6
Time limit: 1 sec
If the next element is the same as the current one, try adjusting the links to remove the next element!
Check if the current element has the same value as the next element, if so adjust the pointers such that the next to next element of the current element is the next element. Refer to the steps below for a better understanding.
The steps are as follows :
O(n), where ‘n’ is the length of the linked list.
Each element in the input list is traversed exactly once, and the links are adjusted accordingly to remove duplicates.
Hence the time complexity is O(n).
O(1)
No extra space is used except ‘cur’.
Hence the space complexity is O(1).