Linked Lists Doubly linked lists
286. Remove Duplicates from a Sorted Doubly Linked List
You are given head, the first node of a doubly linked list whose values are sorted in non-decreasing order,
so each value is greater than or equal to the one before it. Some values appear more than once. Remove the
repeats so that each value appears only once, and return the head of the list.
Each node has a value, val, and two pointers. next points to the following node, and prev points to the
node before it. The head's prev is null, and the last node's next is null. Both directions must still be
right after the repeats are gone. The comment at the top of the starter code shows the node in your language.
The examples write a list as its values from the head on, so head = [4,1,6] is the list 4 ⇄ 1 ⇄ 6, and the
output shows the list you return the same way. An empty list is written []. The judge also checks every
prev pointer. If one is wrong, the output names the first node whose prev is wrong, counting the head as
node 1, instead of the values.
Example 1
Input: head = [2, 2, 5, 7, 7, 7, 9]
[2,5,7,9]Explanation: The second 2 goes, so the first 2 now points forward to 5 and 5 points back to it. The second and third 7 go too, so the first 7 and 9 point at each other.
Example 2
Input: head = [-6, -6, -6, -6]
[-6]Explanation: Every node holds -6, so only the head is left. It is now also the last node, so its next is null.
Example 3
Input: head = [-1000000000, -3, 0, 1000000000]
[-1000000000,-3,0,1000000000]Explanation: No value repeats, so the list comes back unchanged.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
0 ≤ number of nodes ≤ 105-109 ≤ Node.val ≤ 109- The values are sorted in non-decreasing order.
Clarifying questions
What you would ask an interviewer before writing any code.