File: delete-n-nodes-after-m-nodes-of-a-linked-list/solution.py

Date: 2026-06-06

Time: 16:16

Purpose

This file implements LeetCode problem 1474: "Delete N Nodes After M Nodes of a Linked List." It's a self-contained solution module that defines the linked list node type, the core algorithm, conversion helpers, and a full test suite — following the standard layout used across all problems in this repo.

Key Components

ListNode (class)

Standard singly-linked list node with val: int and next: Optional[ListNode]. This is defined locally rather than imported from a shared module, making each solution independently runnable.

deleteNodes(head, m, n) (function)

The core algorithm. Operates in-place on the linked list in a repeating two-phase cycle:

1. Keep phase: Walk forward m - 1 steps (keeping m nodes total, since current is already on the first kept node).

2. Delete phase: Walk a separate skip pointer forward n steps from current.next, then splice by setting current.next = skip.

3. Advance: Move current to skip (the first node after the deleted section) and repeat.

Returns the original head — the head never changes because we always keep the first m nodes.

listtolinked(vals) / linkedtolist(head)

Bidirectional conversion between Python lists and linked lists. Used exclusively by tests to set up inputs and assert outputs without manually constructing node chains.

TestDeleteNodes (class)

Eight test cases covering: the two LeetCode examples, single-node lists, m exceeding list length, n exceeding remaining nodes, alternating keep/delete (m=1, n=1), keeping all nodes, and deleting everything after the first node.

Patterns

Dependencies

Imports: Only stdlib — _future_.annotations (for Optional[ListNode] forward refs), typing.Optional, and unittest. No external packages.

Imported by: The test_solution.py in this same directory imports from it. The massive "Imported By" list in the prompt is misleading — those are cross-references from other problems' test files, likely an artifact of the repo's analysis tooling rather than actual imports of this specific file.

Flow

For input [1,2,3,4,5,6,7,8,9,10,11,12,13], m=2, n=3:


Cycle 1: keep [1,2], delete [3,4,5] → wire 2→6
Cycle 2: keep [6,7], delete [8,9,10] → wire 7→11
Cycle 3: keep [11,12], delete [13] → wire 12→None
Result: [1,2,6,7,11,12]

The keep-phase loop runs m - 1 times (not m) because current starts on the first node to keep. The delete-phase uses a separate skip pointer starting at current.next and advances n times, tolerating early exhaustion via if not skip: break.

Invariants

Error Handling

There is none beyond the null checks on current and skip. The function assumes valid inputs per the LeetCode contract (non-null head, m >= 1, n >= 1). No exceptions are raised. If head is None, the while current loop never executes and None is returned.

Topics to Explore

Beliefs