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.
We'll cover the following
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:
Start with the
head
node and set it as thecurrent
node.Traverse the next
m
nodes from thecurrent
node without deleting them.Traverse the next
n
nodes and delete them.Repeat steps 2 and 3 above until the end of the linked list is reached.
Constraints:
Node
, where Node
is the number of nodes in the list.Node.data
m
,n
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 thehead
of the linked list. It is used to traverse through the list node by node.Initialize a pointer,
lastMNode
, to thehead
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
becomesNone
, 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, updatelastMNode
to always point to the most recent node in the retained group of nodes and move thecurrent
pointer forward. If the end of the list is reached before completingm
nodes, the loop exits early.To ensure that exactly
n
nodes are skipped in the current iteration, enter another loop that runsn
times. Here, move thecurrent
pointer forward, but don’t updatelastMNode
. This will eventually skip then
nodes. If the end of the list is reached before completingn
nodes, the loop exits early.After skipping the
n
nodes, the next valid node pointed to bycurrent
is connected to the retained group of nodes. To do this, update the next pointer oflastMNode
to point to thecurrent
. This will delete the skipped nodes from the list and allow a connection between the previousm
nodes and the nextm
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.