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:
m nodes starting with the current node.n nodesReturn 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:
[1, 104].1 <= Node.val <= 1061 <= m, n <= 1000
Follow up: Could you solve this problem by modifying the list in-place?
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.
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).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Simulation | O(n) | O(1) | Best general solution. Single pass traversal with pointer rewiring. |
| Simulation with Helper Pointer | O(n) | O(1) | When you want clearer separation between keep-phase and delete-phase logic. |
Delete N nodes after M nodes of a linked list | GeeksforGeeks • GeeksforGeeks • 28,183 views views
Watch 6 more video solutions →Practice Delete N Nodes After M Nodes of a Linked List with our built-in code editor and test cases.
Practice on FleetCode