Solution: Delete N Nodes After M Nodes of a Linked List

Let’s solve the Delete N Nodes After M Nodes of a Linked List problem using the in-place manipulation of a Linked List pattern.

Statement

Given the head of a linked list and two integers, m and n, remove some specific nodes from the list and return the head of the modified, linked list. The list should be traversed, and nodes removed as follows:

  1. Start with the head node and set it as the current node.

  2. Traverse the next m nodes from the current node without deleting them.

  3. Traverse the next n nodes and delete them.

  4. Repeat steps 2 and 3 above until the end of the linked list is reached.

Constraints:

  • 11 \leq Node 103\leq 10^3, where Node is the number of nodes in the list.

  • 11 \leq Node.data 103\leq 10^3

  • 11 \leq m, n 500\leq 500

Solution

An optimized way to solve this problem is to modify the linked list by alternately keeping and skipping nodes based on the given values of m and n, respectively. Starting at the head of the list, the first group (containing m nodes) of nodes is traversed and retained. After keeping these m nodes, the next group (containing n nodes) is skipped by advancing the pointer through the list without linking those nodes to the previously retained nodes. Skipping nodes means deleting them from the given list. Once the skipped group is passed, the retained section is reconnected to the remaining nodes, and the process is repeated until the entire list has been processed. After processing all the nodes in the list, return the head of the modified list.

Now, let’s look at the algorithm steps for this solution:

  • Initialize a pointer, current, to the head of the linked list. It is used to traverse through the list node by node.

  • Initialize a pointer, lastMNode, to the head of the linked list. It keeps track of the last node in the current group of `m` nodes that must be retained.

  • Start the traversal until current becomes None, indicating that the end of the linked list is reached. Process the nodes as follows:

    • To ensure that exactly m nodes are retained in the current iteration, enter another loop that runs m times. Here, update lastMNode to always point to the most recent node in the retained group of nodes and move the current pointer forward. If the end of the list is reached before completing m nodes, the loop exits early.

    • To ensure that exactly n nodes are skipped in the current iteration, enter another loop that runs n times. Here, move the current pointer forward, but don’t update lastMNode. This will eventually skip the n nodes. If the end of the list is reached before completing n nodes, the loop exits early.

    • After skipping the n nodes, the next valid node pointed to by current is connected to the retained group of nodes. To do this, update the next pointer of lastMNode to point to the current. This will delete the skipped nodes from the list and allow a connection between the previous m nodes and the next m nodes.

    • Repeat the steps above until the current reaches the end of the list.

  • After the traversal is complete, the function returns the `head` of the modified linked list.

Let’s look at the following illustration to get a better understanding of the solution:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.