Delete a Node at a Given Position in a Linked List

Implement deleteAtPosition

Given the head of a singly linked list and a 0-indexed position, delete the node at that index and return the head of the resulting list. position = 0 is exactly the delete-at-the-beginning case. For any other position, the node being deleted is never visited directly — only the node right before it matters, since deleting means redirecting that node's next pointer straight past the target.

Example 1:

Input: head = [1,2,3,4,5], position = 2

Output: [1,2,4,5]

Example 2:

Input: head = [9], position = 0

Output: []

Example 3:

Input: head = [10,20,30], position = 0

Output: [20,30]

+ 5 hidden test cases run on Submit.

Constraints:

  • 1 ≤ number of nodes in head ≤ 10⁴
  • -10⁹ ≤ node value ≤ 10⁹
  • 0 ≤ position < length of head — position is 0-indexed, so position 0 deletes the head

head =

[1, 2, 3, 4, 5]

position =

2