# 19. Remove Nth Node From End of List

{% embed url="<https://leetcode.com/problems/remove-nth-node-from-end-of-list/>" %}

#### two pointers

```javascript
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
let prev = new ListNode(-1, null);
var removeNthFromEnd = function (head, n) {
    prev.next = head;

    let fast = prev;
    let slow = prev;
    for (let i = 0; i < n; i++) {
        fast = fast.next;
    }

    while (fast.next !== null) {
        fast = fast.next;
        slow = slow.next;
    }

    slow.next = slow.next.next;
    return prev.next;
};
```

#### iteration

> time: O(n)

> space: O(1)

```javascript
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function (head, n) {

    const prev = new ListNode(-1, null);
    prev.next = head;

    let cnt = 0;
    let currentNode = head;
    while (currentNode !== null) {
        currentNode = currentNode.next;
        cnt++;
    }

    currentNode = prev;
    while (cnt - n > 0) {
        currentNode = currentNode.next;
        cnt--;
    }

    currentNode.next = currentNode.next.next;
    return prev.next;
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/top-75-leetcode-questions-to-save-your-time/problems/linked-list/19.-remove-nth-node-from-end-of-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
