Linked Lists Singly linked list basics
270. Delete the K-th Node
You are given head, the first node of a singly linked list, and an integer k. Delete the k-th node of
the list, counting from the head, and return the head of the list that is left.
Positions start at 1: the head is node 1, the node after it is node 2, and so on. So k = 1 deletes the
head, and k equal to the number of nodes deletes the last node.
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. When no nodes are left, return null, which shows as [].
Example 1
Input: head = [12, -5, 30, 8, 19], k = 3
[12,-5,8,19]Explanation: Node 3 holds 30. After the deletion, the node holding -5 links straight to the node holding 8.
Example 2
Input: head = [4, 9, 4, 1], k = 1
[9,4,1]Explanation: Node 1 is the head. Once it is deleted, the list starts at the node holding 9, and that node is the head to return.
Example 3
Input: head = [-7], k = 1
[]Explanation: The only node is deleted. No nodes are left, so the answer is null.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ number of nodes ≤ 105-109 ≤ Node.val ≤ 1091 ≤ k ≤ number of nodes
Clarifying questions
What you would ask an interviewer before writing any code.