19. Remove Nth Node From End of List

202410101650
tags: #linked-list

var removeNthFromEnd = function(head, n) {
	const dummy = new ListNode();
	dummy.next = head;
	let cur = dummy;
	let fast = dummy;
	for (let i = 0; i <= n; i++) {
		fast = fast.next;
	}
	while (fast !== null) {
		cur = cur.next;
		fast = fast.next;
	}
	cur.next = cur.next.next;
	return dummy.next;
};

Reference

https://leetcode.com/problems/remove-nth-node-from-end-of-list/