Linked Lists Doubly linked lists
281. Delete the Tail of a Doubly Linked List
You are given head, the first node of a doubly linked list. Delete the last node of the list, the
tail, and return the head of the list that is left.
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 tail's next is null. The list you return must follow the
same rules. The comment at the top of the starter code shows the node in your language.
A list of one node has nothing left once its tail is gone, so return an empty list (null). An empty list has no tail, so return it as it is.
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. 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 = [14, -6, 23, 8, 2]
[14,-6,23,8]Explanation: The tail holds 2, and the node before it holds 8. That node's next becomes null, so it is the new tail.
The head is still 14.
Example 2
Input: head = [-3, 11, -3]
[-3,11]Explanation: The tail holds -3, and so does the head. Only the last node goes; the head stays where it is.
Example 3
Input: head = [42]
[]Explanation: The only node is both the head and the tail. Once it is gone the list is empty, so the answer is null.
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
Clarifying questions
What you would ask an interviewer before writing any code.