Skip to main content
LeetCode 1474, Easy. Topics: Linked List. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:

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
Example 2

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026