Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Insertion in Singly Linked List in JAVA

How to insert an element in a Linked List

Insertion in a Singly Linked List is one of the primary operation that we can perform on Singly Linked List in Java. Singly Linked List is a structure that is made up of nodes, which is enabled to store data and the address of the next node. Insertion in a Singly Linked List in Java can be performed in 3 ways, they are -:

  • Insertion at beginning
  • Insertion at end
  • Insertion at nth position

In this article we will learning a brief about all the three types.

Insertion in Singly Linked List in Java

How do you add an element to a Singly Linked List ?

Adding data in a Singly Linked List is practically a bit easier than the other data structures. As in Singly Linked List, we does not need to traverse the whole data structure for entering a node, we just need to update the address of the last  node which is currently pointing to null, or some other node, with the new node that we want to insert. There are three different ways in which we can insert a node in a linked list -:

  1. Insertion at beginning
  2. Insertion at end
  3. Insertion at nth position

Definition of Singly linked list in JAVA

struct Node 
{
  int Data; //for storing a specific data
  Struct Node* ptr; //for storing the address of the next node
};

Inserting an element in the beginning of the Singly Linked List

To perform insertion at beginning in singly linked list we will use the following steps:-

  • We will create a space at beginning, before head node.
  • In this space created we will built a new node.
  • After that we will but the data in the new node that we want to insert.
void insertionatBeginning(int data) 
{
    struct node *NodeatBeginning;
    frntNode = (struct node*)malloc(sizeof(struct node));
    if(NodeatBeginning == NULL)
    {
        System.out.println(" Memory can not be allocated\n");
    }
    else 
    { 
        NodeatBeginning ->num = num;         
        NodeatBeginning ->nextptr = stnode; 
        stnode = NodeatBeginning; 
    }
}
Java program for insertion at beginning in Singly linked list

Inserting an element in the end of the Singly Linked List

To perform insertion at end in singly linked list we will use the following steps:-

  • In the process of Insertion at the Last Position we’ll begin by checking whether if the linked list is empty.
  • If the head is equal to null, then the list is already empty else the list already has an element.
  • In Order to insert a node in the end we’ll first store the current pointer address of null from the current last node to a temporary variable.
  • The value of that temporary variable will now be stored in the address of the new node being inserted.
  • Followed by which we’ll store the address of the new node into the previous last node of the linked list.
void Insertion_at_End(int num)
{
  struct node *First_Node, *tmp;
  First_Node = (struct node *) malloc (sizeof (struct node));
  if (First_Node == NULL)
    {
      System.out.println("Memory can not be allocated.");
    }
  else
    {
      First_Node ->num = num;
      First_Node ->nextptr = NULL;
      tmp = stnode;
      while (tmp->nextptr != NULL)
	tmp = tmp->nextptr;
      tmp->nextptr = First_Node;
    }
Java program for insertion at end in Singly linked list

Inserting an element in the nth position of the Singly Linked List

To perform insertion at a specific position in singly linked list we will use the following steps:-

  • First we will create a new node named by newnode and put the position where you want to insert the node.
  • Now give the address of the new node in previous node means link the new node with previous node.
  • After this, give the address of current node in new node. Means link your new node also with current node.
void Insertion_at_specific_position(int num, int pos)
{
    int i;
    struct node *Front_Node, *temp;
    frntNode = (struct node*)malloc(sizeof(struct node));
    if(Front_Node == NULL)
    {
       System.out.println("Memory can not be allocated.")
        
    } 
    else
    { 
        Front_Node ->num = num;  
        Front_Node ->nextptr = NULL;
        tmp = stnode;
        for(i=2; i<=pos-1; i++)
        { 
            temp = temp->nextptr;
 
            if(temp == NULL)
                break;
        }
        if(temp != NULL)
        {
            Front_Node ->nextptr = temp->nextptr;  
            temp->nextptr = Front_Node;   
        }
        else
        {
            System.out.println("Data cannot be entered in that particular position\n");
        }
    }
}
Java program for insertion at nth position in Singly linked list