Delete N Nodes After M Nodes of a Linked List

EasyLinked Listsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given the head of a linked list and two positive integers m and n. Starting at the head, repeat the following until the list ends: keep the next m nodes, then delete the n nodes after them. If fewer than m nodes remain, keep them all. If fewer than n nodes remain to delete, delete them all. Return the head of the modified list.

Examples

Input: head = [7,3,9,1,4,8,2,6,5], m = 2, n = 1
Output: [7,3,1,4,2,6]
Keep 7,3, delete 9; keep 1,4, delete 8; keep 2,6, delete 5.
Input: head = [1,2,3,4,5], m = 1, n = 3
Output: [1,5]
Keep 1, delete 2,3,4, then keep 5.
Input: head = [4,5,6], m = 3, n = 2
Output: [4,5,6]
All three nodes are kept before any deletion happens.

Constraints

  • 1 <= number of nodes <= 10^4
  • 1 <= Node.val <= 10^6
  • 1 <= m, n <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems