Delete Nth Node from End of Linked List

Languages

Given the head of a linked list, delete the n-th node from the end of the list and return the updated head.

The linked list is represented by a sequence of ListNodes, where each node points to the next node in the sequence, or null if it is the last node.

A ListNode has the following interface:

interface ListNode {
val: number;
next: ListNode | null;
}

Input

  • head: ListNode: Head of the linked list. Examples display each linked list as an array of values within the list
  • n: number: An integer indicating the position (from the end) of the node to delete

Examples

Input: list = [1,2,3,4,5,6], n = 3
Output: [1,2,3,5,6]
Explanation: The 3rd node from the end is 4. Deleting it results in the list [1, 2, 3, 5, 6].
Input: list = [8], n = 1
Output: []
Explanation: The list contains only one node, which is the 1st from the end. Deleting it results in an empty list.
Input: list = [9,7], n = 1
Output: [9]
Explanation: The 1st node from the end is 7. Deleting it results in the list [9].

Constraints

  • 1 <= Number of nodes <= 1000
  • 1 <= ListNode.val <= 1000