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

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

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: