Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Delete X Nodes After Y Nodes In a Linked List

Objec­tive: Given a Linked List and x and y. Delete x num­ber of nodes after y nodes from the start.

Exam­ple:

->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 start­ing from the head).
  • Another pointer at one node after to the nodes to be deleted. (Move it by x start­ing from the y, as per pre­vi­ous step).
  • Then Just link these two nodes.

Com­plete Code:


Out­put:

->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

You may also like...