Here’s the problem:
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:
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.
- If cur == None, break out of the loop.
- If we are on the first node, make last.next = cur.
- 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:
- if cur == None, break out of the loop
- We set last.next = 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:
Don’t hesitate to reach out to me!