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

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
public Node insertBeginning(int data)
{
    Node newNode = new Node(data);
    newNode.next = head;
    head = newNode;

    return head;
}
static Node insertBeginning(Node head, int data)
{
    Node newNode = new Node(data);

    newNode.next = head;
        head = newNode;

    return head;
}

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
public void insertEnd(int data)
{
    Node newNode = new Node(data);

    if(head==null)
    {
       head = newNode;
       return;
    }

    Node temp = head;

    while(temp.next!=null)
        temp = temp.next;

    temp.next = newNode;
}
static Node insertEnd(Node head, int data) {

    Node newNode = new Node(data);

    if(head==null) {
        head = newNode;
        return head;
    }

    Node temp = head;

    while(temp.next!=null)
       temp = temp.next;

    temp.next = newNode;
    return head;
}

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
public void insertAfter(int n,int data)
{
    int size = calcSize(head);

    // Can only insert after 1st position
    // Can't insert if position to insert is greater than size of Linked List
    if(n < 1 || n > size)
    {
        System.out.println("Can't insert\n");

    }
    else
    {
        Node newNode = new Node(data);
        // required to traverse
        Node temp = head;

        // traverse to the nth node
        while(--n > 0)
            temp=temp.next;

        newNode.next= temp.next;
        temp.next = newNode;
    }
}
static Node insertAfter(int pos, int data, Node head) {
    int size = calcSize(head);

    // Can only insert after 1st position
    // Can't insert if position to insert is greater than size of Linked List
    if (pos < 1 || size < pos) {
        System.out.println("Can't insert," + pos + " is not a valid position\n");
    } else {
        Node newNode = new Node(data);

        // required to traverse Node 
        Node temp = head;

        // traverse to the nth node
        while (--pos > 0)
            temp = temp.next;

        newNode.next = temp.next;
        temp.next = newNode;
    }
    return head;
}

Full Code for Insertion in Java

Let us have a look at the code below –

import java.lang.*;

class LinkedList {
    Node head;

    // Node Class
    class Node{
        int data;
        Node next;

        Node(int x)
        {
            data = x;
            next = null;
        }
    }
    public Node insertBeginning(int data)
    {
        Node newNode = new Node(data);
        newNode.next = head;
        head = newNode;

        return head;
    }

    public void insertEnd(int data)
    {
        Node newNode = new Node(data);

        if(head==null)
        {
            head = newNode;
            return;
        }

        Node temp = head;

        while(temp.next!=null)
            temp = temp.next;

        temp.next = newNode;

    }

    public void insertAfter(int n,int data)
    {
        int size = calcSize(head);

        // Can only insert after 1st position
        // Can't insert if position to insert is greater than size of Linked List
        if(n < 1 || n > size)
        {
            System.out.println("Can't insert\n");

        }
        else
        {
            Node newNode = new Node(data);
            // required to traverse
            Node temp = head;

            // traverse to the nth node
            while(--n > 0)
                temp=temp.next;

            newNode.next= temp.next;
            temp.next = newNode;
        }
    }

    public void display()
    {
        Node node = head;
        //as linked list will end when Node reaches Null
        while(node!=null)
        {
            System.out.print(node.data + " ");
            node = node.next;
        }
        System.out.println();
    }

    public int calcSize(Node node){
        int size = 0;
        while(node!=null){
            node = node.next;
            size++;
        }
        return size;
    }

    public static void main(String args[])
    {
        LinkedList listObj = new LinkedList();

        listObj.insertBeginning(15);
        listObj.insertBeginning(10);
        listObj.insertBeginning(5);

        listObj.display();

        listObj.insertEnd(20);
        listObj.insertEnd(25);
        listObj.insertEnd(30);
        listObj.insertEnd(35);

        listObj.display();

        listObj.insertAfter(3,100);

        listObj.display();
    }
}

Output

5 10 15 
5 10 15 20 25 30 35 
5 10 15 100 20 25 30 35 
import java.lang.*;

// Node Class
class Node {
    int data;
    Node next;

    Node(int x)
    {
        data = x;
        next = null;
    }
}

class Main
{
    static Node insertBeginning(Node head, int data)
    {
        Node newNode = new Node(data);

        newNode.next = head;
        head = newNode;

        return head;
    }

    static Node insertEnd(Node head, int data) {

        Node newNode = new Node(data);

        if(head==null) {
            head = newNode;
            return head;
        }

        Node temp = head;

        while(temp.next!=null)
            temp = temp.next;


        temp.next = newNode;
        return head;
    }

    static Node insertAfter(int pos, int data, Node head) {
        int size = calcSize(head);

        // Can only insert after 1st position
        // Can't insert if position to insert is greater than size of Linked List
        if(pos < 1 || size < pos) { 
System.out.println("Can't insert," + pos + " is not a valid position\n");
}
else {
Node newNode = new Node(data);
// required to traverse Node Node temp = head;
// traverse to the nth node
while (--pos > 0) temp = temp.next; newNode.next= temp.next; temp.next = newNode; } return head; } static void display(Node node) { while (node != null) { System.out.print(node.data + " "); node = node.next; } System.out.println(); } static int calcSize(Node node){ int size=0; while(node!=null) { node = node.next; size++; } return size; } public static void main(String args[]) { Node head = null; head = insertBeginning(head,15); head = insertBeginning(head,10); head = insertBeginning(head,5); display(head); head = insertEnd(head,20); head = insertEnd(head,25); head = insertEnd(head,30); head = insertEnd(head,35); display(head); //Inserts after 3rd position head = insertAfter(3,100,head); display(head); } }

Output

5 10 15 
5 10 15 20 25 30 35 
5 10 15 100 20 25 30 35 

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java