> ## 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.

# Delete N Nodes After M Nodes of a Linked List

> Tested Python solution for LeetCode 1474 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1474, [Easy](/catalog/easy). Topics: [Linked List](/catalog/topics/linked-list). [View on LeetCode](https://leetcode.com/problems/delete-n-nodes-after-m-nodes-of-a-linked-list/description/).

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

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

## Problem

You are given the `head` of a linked list and two integers `m` and `n`.

Traverse the linked list and remove some nodes in the following way:

* Start with the head as the current node.
* Keep the first `m` nodes starting with the current node.
* Remove the next `n` nodes
* Keep repeating steps 2 and 3 until you reach the end of the list.

Return *the head of the modified list after removing the mentioned nodes*.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1400-1499/1474.Delete%20N%20Nodes%20After%20M%20Nodes%20of%20a%20Linked%20List/images/sample_1_1848.png)

```
Input: head = [1,2,3,4,5,6,7,8,9,10,11,12,13], m = 2, n = 3
Output: [1,2,6,7,11,12]
Explanation: Keep the first (m = 2) nodes starting from the head of the linked List (1 -> 2) show in black nodes.
Delete the next (n = 3) nodes (3 -> 4 -> 5) show in red nodes.
Continue with the same procedure until reaching the tail of the Linked List.
Head of the linked list after removing nodes is returned.
```

![Example 2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1400-1499/1474.Delete%20N%20Nodes%20After%20M%20Nodes%20of%20a%20Linked%20List/images/sample_2_1848.png)

```
Input: head = [1,2,3,4,5,6,7,8,9,10,11], m = 1, n = 3
Output: [1,5,9]
Explanation: Head of linked list after removing nodes is returned.
```

### Constraints

* The number of nodes in the list is in the range `[1, 10^4]`.
* `1 <= Node.val <= 10^6`
* `1 <= m, n <= 1000`

**Follow up:** Could you solve this problem by modifying the list in-place?

## Solution

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

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


class Solution:
    # Time: O(len(head))
    # Space: O(1)
    def delete_nodes(self, head: ListNode[int] | None, m: int, n: int) -> ListNode[int] | None:
        pre = head
        while pre:
            for _ in range(m - 1):
                if pre.next:
                    pre = pre.next
            cur = pre
            for _ in range(n):
                if cur.next:
                    cur = cur.next
            pre.next = cur.next
            pre = pre.next
        return head
```

## Complexity

| Time | Space |
| - | - |
| O(len(head)) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.