Fundamentals/Delete Node Without Head Access
← PrevNext →
Given the head of a linked list and a position, delete the node at that position without using the previous node.
In the original interview variant, you're given only a pointer to the node to delete (not the head). In this course version, we provide head and position so you can locate that node first, then apply the same deletion trick on the node itself.
Input
head: the head node of a singly linked list
position: an integer representing the position of the node to delete (1-indexed)
Output
Return the head of the modified linked list
Constraints
The list has at least 2 nodes
The node to delete is not the last node
Position is valid (1 <= position < length of list)
Examples
Example 1
Inputhead = [1, 2, 3, 4], position = 2
Output[1, 3, 4]
ExplanationDelete node at position 2 (value 2)
Example 2
Inputhead = [4, 5, 1, 9], position = 3
Output[4, 5, 9]
ExplanationDelete node at position 3 (value 1)