Insert a node at a given position in a Linked List in Java

Java Program to Insert node at a specific position

In this blog post, we will write a program to insert a node at a given position in Linked List in Java.

Let’s have a look at Program to Insert a node at given position in Linked list in Java

insert node

Approach

  • Accept the position at which the user wants to enter the new node call this position : pos
  • Visit the node just before that position i.e. pos – 1 node
  • Change the next node of pos – 1 node this new node
  • Change this new node’s next node to pos + 1 node
Insertion at a specific node ina a Linked List in java

Program to Insert a node at given position in linked list in Java

Run
import java.lang.*;

class LinkedList {
    Node head;
    int size = 0;
    // Node Class
    class Node{
        int data;
        Node next;

        Node(int x) // parameterized constructor
        {
            data = x;
            next = null;
        }
    }
    public void insert(int data)
    {
        Node new_node = new Node(data);

        new_node.data = data;
        new_node.next = head;
        head = new_node;
        size++;
    }
    public void insertPosition(int pos, int data) {
        Node new_node = new Node(data);
        new_node.data = data;
        new_node.next = null;

        // Invalid positions
        if(pos < 1 || pos > size + 1)
            System.out.println("Invalid\n");

            // inserting first node
        else if(pos == 1){
            new_node.next = head;
            head = new_node;
            size++;
        }

        else
        {
            Node temp = head;

            // traverse till the current (pos-1)th node
            while(--pos > 1){
                temp = temp.next;
            }
            new_node.next= temp.next;
            temp.next = new_node;
            size++;
        }
    }

    public void display()
    {
        System.out.print("Linked List : ");

        Node node = head;
        // as linked list will end when Node is Null
        while(node!=null){
            System.out.print(node.data + " ");
            node = node.next;
        }
        System.out.println();
    }
}
public class Main{
    public static void main(String args[])
    {
        LinkedList linked_list = new LinkedList();

        linked_list.insert(7);
        linked_list.insert(6);
        linked_list.insert(4);
        linked_list.insert(3);
        linked_list.insert(1);

        linked_list.display();

        // Inserts value: 2 at 2nd position
        linked_list.insertPosition(2, 2);

        // Inserts value: 5 at 5th position
        linked_list.insertPosition(5, 5);

        // Inserts value: 8 at 8th position
        linked_list.insertPosition(8, 8);

        linked_list.display();
    }
}

Output

Linked List : 1 3 4 6 7 
Linked List : 1 2 3 4 5 6 7 8

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

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