Insertion in 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.

    In this article we will learn about all the types of insertion in linked list.

    Insert in Linked

    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
    Singly Linked List Insertion in Java

    Definition of Singly linked list in JAVA

    Singly Linked List Insertion in Java Example
    // Node Class
    class Node{
        int data;
        Node next;
    
        Node(int x) // parameterized constructor
        {
            data = x;
            next = null;
        }
    };

    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.
    Singly Linked List Insertion in Java

    Singly Linked List insertion in Java at the End

    To perform Singly Linked List insertion in Java at the end in a 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.
    Singly Linked List Insertion in Java 2

    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.
    Singly Linked List Insertion in Java 3

    Full Code for Insertion in Java

    Let us have a look at the code below –

    Prime Course Trailer

    Related Banners

    Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

    Get over 200+ course One Subscription

    Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

    Checkout list of all the video courses in PrepInsta Prime Subscription

    Checkout list of all the video courses in PrepInsta Prime Subscription