Delete N Nodes After M Nodes of a Linked List — LeetCode

Here’s the problem:

LeetCode #1474

There are definitely more “elegant” solutions to this problem. My code solution is very “brute-forcy”.

Regardless, it’s faster than 90% of other submissions, so here’s the code:

My Solution

There are two loops in this method. The first loop will loop over nodes that we should not delete. While it’s looping, there are several checks.

  1. If cur == None, break out of the loop.
  2. If we are on the first node, make = cur.
  3. If we are on the last node that we should NOT delete, save it to last = cur.

The delete loop is easier, we only do a few things:

  1. if cur == None, break out of the loop
  2. We set = None, this way we don’t save values we shouldn’t keep.

After all of this, we should have a working solution. Here is the submission report for the above solution:

Faster than 88.93% of all solutions.

As always, my source code is located here and my social links are located here.

Don’t hesitate to reach out to me!

I like to make things.