Delete X Nodes After Y Nodes In a Linked List

Objective: Given a Linked List and x and y. Delete x number of nodes after y nodes from the start.

Example:

->10->20->30->40->50->60->70->80->90->100->110->120
Deleted 4 Nodes after 5 Nodes
->10->20->30->40->50->100->110->120

Delete X Nodes After Y Nodes

Approach:

  • We need two pointers.
  • One pointer at one node prior to the nodes to be deleted. ( Move it by y starting from the head).
  • Another pointer at one node after to the nodes to be deleted. (Move it by x starting from the y, as per previous step).
  • Then Just link these two nodes.

Complete Code:


Output:

->10->20->30->40->50->60->70->80->90->100->110->120
Deleted 4 Nodes after 5 Nodes
->10->20->30->40->50->100->110->120

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: