Skip to main content
data-structure-logo

Linked List – Insertion and Deletion at Nth position

Overview

We discussed how to implement the Linked List in here. In this tutorial, we’re going to implement insertion or deletion a Node at given position.

Insert a Node at Nth Position

  • If Head is null and position is not 0. Then exit it.
  • If Head is null and position is 0. Then insert new Node to the Head and exit it.
  • If Head is not null and position is 0. Then the Head reference set to the new Node. Finally, new Node set to the Head and exit it.
  • If not, iterate until finding the Nth position or end.

 

Delete a Node at Nth Position

  • If Head is null then exit.
  • if the position is 0. Then take next node of Head and assign to the Head and exit.
  • If the Head is not null and position is not 0 then iterate until finding the Nth position. If found it then it will be deleted.

Complete Example

Output: –

10->20->30->NULL
10->20->25->30->NULL
10->20->25->NULL



Related Post

17672total visits,1visits today

RSS
Follow by Email
Facebook
Facebook
Google+
http://mydevgeek.com/linked-list-insertion-deletion-nth-position
Twitter