Skip to main content

Delete N Nodes After M Nodes of a Linked List - Solution & Explanation

EasyPremiumFree on FleetCodeLinked List10 min readAsked at: Microsoft
Practice this problem

Problem Statement

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.

 

Example 1:

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 read 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:

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, 104].
  • 1 <= Node.val <= 106
  • 1 <= m, n <= 1000

 

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

Approach Overview

Problem Overview: Given a linked list, keep the first m nodes, delete the next n nodes, and repeat this pattern until the end of the list. The structure of the list must be modified in-place by updating node pointers.

Approach 1: Iterative Simulation (O(n) time, O(1) space)

The most direct strategy is to simulate the process exactly as described. Start from the head and iterate through the list using a pointer. First move forward m-1 steps to reach the last node that should be kept. From that node, skip the next n nodes by advancing another pointer and reconnect the kept node to the node that appears after the deleted segment. This works because a linked list allows efficient pointer rewiring without shifting elements. The entire list is traversed once, giving O(n) time complexity and O(1) extra space.

The key insight is that deletion in a singly linked list does not require removing nodes individually. Instead, you simply move a pointer n steps ahead and reconnect the previous node to the next valid node. This avoids repeated deletion operations and keeps the logic linear and efficient.

Approach 2: Simulation with Helper Pointer (O(n) time, O(1) space)

A small variation introduces a helper pointer to handle the skipping phase more cleanly. After traversing m nodes with the main pointer, use another pointer to advance through the next n nodes. Once the deletion block ends, update current.next to the node reached by the helper pointer. This separates the "keep" phase and "delete" phase, which can make the code easier to reason about during interviews.

This approach still performs a single pass over the list. Each node is visited at most once while either being counted among the kept nodes or skipped among the deleted nodes. The algorithm remains O(n) time and O(1) space since no additional data structures are required.

Both strategies rely purely on pointer traversal, a core skill when working with linked list problems. The pattern is similar to pointer-walking techniques used in two pointers problems, except both pointers move forward along the same list rather than from opposite ends.

Recommended for interviews: The iterative simulation approach is what interviewers typically expect. It directly mirrors the problem statement and demonstrates comfort with pointer manipulation in linked lists. Explaining how pointer rewiring skips entire blocks of nodes shows a clear understanding of list structure and leads to the optimal O(n) time, O(1) space solution.

Solution

We can simulate the entire deletion process. First, use a pointer pre to point to the head of the linked list, then traverse the linked list, moving m - 1 steps. If pre is null, it means the number of nodes from the current node is less than m, so we directly return the head. Otherwise, use a pointer cur to point to pre, then move n steps. If cur is null, it means the number of nodes from pre is less than m + n, so we directly set the next of pre to null. Otherwise, set the next of pre to the next of cur, then move pre to its next. Continue traversing the linked list until pre is null, then return the head.

The time complexity is O(n), where n is the number of nodes in the linked list. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative SimulationO(n)O(1)Best general solution. Single pass traversal with pointer rewiring.
Simulation with Helper PointerO(n)O(1)When you want clearer separation between keep-phase and delete-phase logic.

Video Solution

leetcode - 1474 Delete N Nodes After M Nodes of a Linked List • litCode • 1,714 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Delete N Nodes After M Nodes of a Linked List easy or hard?
The problem is generally classified as Easy because it involves straightforward pointer traversal in a linked list. The main challenge is correctly handling pointer updates while skipping nodes and ensuring edge cases like reaching the end of the list are handled.
Delete N Nodes After M Nodes of a Linked List Python/Java solution
Implement the solution by iterating through the linked list and using pointer updates. Move m steps forward to keep nodes, then advance another pointer n steps to skip nodes, and reconnect the list. The same logic works across Python, Java, C++, Go, and TypeScript with O(n) time and O(1) space.
How to solve Delete N Nodes After M Nodes of a Linked List in O(n)?
Traverse the list with a pointer and first move forward m nodes to keep them. From the m-th node, advance another pointer n nodes ahead to skip the nodes that should be deleted. Reconnect the current node's next pointer to the node after the skipped segment and continue the process until the end of the list.
What is the best approach for Delete N Nodes After M Nodes of a Linked List?
The best approach is iterative simulation using pointer traversal. Traverse the linked list, keep the first m nodes, then skip the next n nodes by advancing a pointer and reconnecting the list. This processes each node at most once, resulting in O(n) time complexity and O(1) extra space.
Is Delete N Nodes After M Nodes of a Linked List asked at Google/Amazon/Meta?
Linked list pointer manipulation problems like this appear frequently in interviews at companies such as Amazon, Google, and Meta. While this exact question may vary, similar patterns involving node skipping, deletion, and pointer rewiring are common interview topics.
What data structure is used in Delete N Nodes After M Nodes of a Linked List?
The problem uses a singly linked list. The solution relies on pointer traversal and pointer reassignment to remove segments of nodes without allocating additional memory.
What is the time complexity of Delete N Nodes After M Nodes of a Linked List?
The optimal solution runs in O(n) time, where n is the number of nodes in the linked list. Each node is visited at most once while either being counted among the kept nodes or skipped during deletion. The space complexity is O(1) because the algorithm only uses a few pointers.

Ready to solve this problem?

Practice Delete N Nodes After M Nodes of a Linked List with our built-in code editor and test cases.

Practice on FleetCode