> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Remove Nth Node From End of List

> Tested Python solution for LeetCode 19 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 19, Medium. Topics: Linked List, Two Pointers. [View on LeetCode](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 19   # by problem number
lcpy gen -s remove_nth_node_from_end_of_list   # by problem name
```

## Problem

Given the `head` of a linked list, remove the `n<sup>th</sup>` node from the end of the list and return its head.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)

```
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]
```

### Constraints

* The number of nodes in the list is `sz`.
* `1 <= sz <= 30`
* `0 <= Node.val <= 100`
* `1 <= n <= sz`

**Follow up:** Could you do this in one pass?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_nth_node_from_end_of_list/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_nth_node_from_end_of_list/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(L) where L is the length of the list
    # Space: O(1)
    def remove_nth_from_end(self, head: ListNode[int] | None, n: int) -> ListNode[int] | None:
        dummy = ListNode(0)
        dummy.next = head
        fast: ListNode[int] | None = dummy
        slow: ListNode[int] | None = dummy

        # Move fast pointer n+1 steps ahead
        for _ in range(n + 1):
            assert fast
            fast = fast.next

        # Move both pointers until fast reaches end
        while fast:
            fast = fast.next
            assert slow
            slow = slow.next

        # Remove the nth node
        assert slow and slow.next
        slow.next = slow.next.next
        return dummy.next
```

## Complexity

| Time                                   | Space |
| -------------------------------------- | ----- |
| O(L) where L is the length of the list | O(1)  |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
