Delete N Nodes After M Nodes of a Linked List - 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.

Input & Output

Example 1 — Basic Pattern
$ Input: head = [1,2,3,4,5,6,7,8,9,10,11], m = 2, n = 3
Output: [1,2,6,7,10,11]
💡 Note: Keep first 2 nodes (1,2), delete next 3 (3,4,5), keep next 2 (6,7), delete next 3 (8,9,10), keep remaining (11)
Example 2 — Short List
$ Input: head = [1,2,3,4], m = 1, n = 2
Output: [1,4]
💡 Note: Keep 1 node (1), delete next 2 (2,3), keep remaining (4)
Example 3 — Delete All After Keep
$ Input: head = [1,2,3], m = 1, n = 3
Output: [1]
💡 Note: Keep 1 node (1), delete next 3 but only 2,3 exist, so final result is [1]

Constraints

  • The number of nodes in the given linked list is in the range [1, 104].
  • 1 ≤ Node.val ≤ 106
  • 1 ≤ m, n ≤ 1000

Visualization

Tap to expand
Delete N Nodes After M Nodes: Pattern VisualizationInput List:1234567Pattern: m=2, n=3KEEP 2DELETE 3KEEP 2Output List:12673, 4, 5 deleted
Understanding the Visualization
1
Input
Original linked list with m=2, n=3 parameters
2
Pattern
Apply keep 2, delete 3 repeatedly
3
Output
Final modified linked list
Key Takeaway
🎯 Key Insight: Process the linked list in repeating cycles rather than checking each node individually
Asked in
Microsoft 15 Amazon 12 Facebook 8 Google 6
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen