Published Nov 30, 2021
[
 
]
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Input: head = [1], n = 1
Output: []
Input: head = [1,2], n = 1
Output: [1]
- The number of nodes in the list is sz.
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= sz
n + 1
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
// create a dummy node pointing to head
const dummy = new ListNode(0, head)
let left = dummy;
let right = head;
// make sure the diff between left & right is n + 1
while(n > 0 && right) {
right = right.next;
n--;
}
// move both right & left to next until right is null
// then we have left before the nth node from the end
while(right) {
left = left.next
right = right.next
}
// deletion
left.next = left.next.next
return dummy.next
};