Insert node at Last / End Position in Singly Linked List [SLL]

April 26, 2025 6 Comments » Hits : 1,070





Insert node at Last / End Position in Singly Linked List


Inserting node at start in the SLL (Steps):

  1. Create New Node
  2. Fill Data into “Data Field
  3. Make it’s “Pointer” or “Next Field” as NULL
  4. Node is to be inserted at Last Position so we need to traverse SLL upto Last Node.
  5. Make link between last node and newnode
void insert_at_end()
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
if(new_node == NULL)
   printf("nFailed to Allocate Memory");
 printf("nEnter the data : ");
 scanf("%d",&new_node->data);
 new_node->next=NULL;
 if(start==NULL)
 {
   start=new_node;
   current=new_node;
 }
 else
 {
   temp = start;
     while(temp->next!=NULL)
     {
     temp = temp->next;
     }
   temp->next = new_node;
 }
}

Diagram :

Attention :

  1. If starting node is not available then “Start = NULL” then following part is executed
if(start==NULL)
       {
       start=new_node;
       current=new_node;
       }
  1. If we have previously created First or starting node then “else part” will be executed to insert node at start
  2. Traverse Upto Last Node., So that temp can keep track of Last node
else
       {
       temp = start;
   while(temp->next!=NULL)
  {
  temp = temp->next;
  }
  1. Make Link between Newly Created node and Last node ( temp )
temp->next = new_node;

To pass Node Variable to Function Write it as -

void insert_at_end(struct node *temp)

Incoming search terms:

  • ab

    We should thank you for giving such a wonderful blog. Your site happens to be not only informative but also very imaginative too. We find a limited number of experts who can think to write technical articles that creatively. All of us are on the lookout for information on something like this. I Myself have gone through several blogs to build up on knowledge about this.We look forward to the next posts !!

  • Priteh Taral

    Thank you for encouraging us !!!

  • Anonymous

    yinyang// August 04 2010

    Your blog is unbelievable and awesome. I hope you can tell more about Data Structures and Algorithms. Keep up the Good work and Thank You very much.

    ^ ^

  • Priteh Taral

    @Yinyang
    You are most welcome

  • raj

    really great site

  • preety

    nice post