FunctionRacerProblemsRace

Keep M Delete N

EasyLinked listsPython, JavaScript, TypeScript, Java, C++

Walk the list in rounds: keep the next m nodes, then delete the next n nodes, then repeat until the list ends. A round may be cut short by the end of the list. Return the head. The list is passed in as head.

Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.

Example 1

head [1,2,3,4,5,6,7]
m 2
n 1
returns [1,2,4,5,7]

Keep 1,2 delete 3, keep 4,5 delete 6, keep 7.

Example 2

head [1,2,3,4]
m 1
n 3
returns [1]

Keep 1, delete 2,3,4.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Linked lists problems