Linked Lists Singly linked list basics
269. Delete the Tail
You are given head, the first node of a singly linked list. Delete the last node of the list, the
tail, and return the head of the list that is left.
Each node holds a value and a pointer to the next node, and the tail's pointer is null. 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. The
output shows the list you return the same way.
Example 1
Input: head = [12, -4, 30, 7, 1]
[12,-4,30,7]Explanation: The tail holds 1. The node holding 7 now points to null, so it becomes the new tail. The head is still 12.
Example 2
Input: head = [9, 2, 9]
[9,2]Explanation: The tail holds 9, and so does the head. Only the last node goes; the head stays where it is.
Example 3
Input: head = [5]
[]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.