Insertion at nth position in Singly Linked List in Java

Insertion at nth Position in JAVA

Let’s have a look at the program of Insertion at nth position in Singly linked list in Java.

insertion at nth position in linked list

Implementation

For Insertion at nth position in singly Linked List in Java we’ll have to follow these steps:

  • First, Traverse the list from head to n-1 position
  • After traversing the list add the new node and allocate memory space to it.
  • Point the next pointer of the new node to the next of current node.
  • Point the next pointer of current node to the new node.
Insertion at nth position in Singly Linked List

Algorithm for Insertion at nth Position in Singly Linked List

  • IF HEAD == NULL
    EXIT
  • ELSE
  • NEW_NODE = ADDNODE()
  • NEW_NODE -> DATA = ITEM
  • SET TEMP = HEAD
  • SET I = 0
  • REPEAT
  • TEMP = TEMP → NEXT
  • IF TEMP = NULL
  • EXIT
  • PTR → NEXT = TEMP → NEXT
  • TEMP → NEXT = PTR
  • SET PTR = NEW_NODE
  • EXIT

Code for Insertion in a Linked List in Java at a Given Node

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

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

One comment on “Insertion at nth position in Singly Linked List in Java”


  • Sagar

    public class SinglyLinkedListPractice {
    Node head;
    //creating of Node in Singly linked list
    class Node
    {
    String data;
    Node next;

    Node(String data)
    {
    this.data=data;
    this.next=null;
    }
    }
    //Add fist operation
    public void addFirst(String data){
    Node newNode=new Node(data);
    if (head==null){
    head=newNode;
    return;
    }
    newNode.next=head; // set the newNode.next as a head
    head=newNode; // Now the head is point to the newNode
    }

    //Add Last operation
    public void addLast(String data){
    Node newNode=new Node(data);
    if (head==null)
    {
    head=newNode;
    return;
    }
    Node currNode=head;
    while (currNode.next != null)
    {
    currNode=currNode.next; //upadate the current node as a next node till currNode.next == null
    }
    currNode.next=newNode;
    }
    //Print operation of sll
    public void printList()
    {
    if (head==null)
    {
    System.out.println(“The linked list is empty”);
    return;
    }
    Node currNode=head;
    while (currNode != null)
    {
    System.out.print(currNode.data+” -> “);
    currNode=currNode.next;
    }
    System.out.println(“NULL”);
    }
    //Check the size of Linked list
    public int checkSize()
    {
    if (head==null)
    {
    System.out.println(“The linked list empty”);
    }
    Node currNode=head;
    int count=0;
    while (currNode.next != null)
    {
    count++;
    currNode=currNode.next;
    }
    return count;
    }

    //insertion at specific position
    public void insertSpecific(String data,int position)
    {
    Node newNode=new Node(data);
    if (position checkSize())
    {
    System.out.println(“Invalid position”);
    }
    Node currNode=head;
    int i=0;
    while (i<position)
    {
    currNode=currNode.next;
    i++;
    }
    if (i==position)
    {
    newNode.next=currNode.next;
    currNode.next=newNode;
    }
    }

    public static void main(String[] args) {
    SinglyLinkedListPractice sll=new SinglyLinkedListPractice();
    sll.addFirst("10");
    sll.addFirst("20");
    sll.printList();
    sll.addLast("30");
    sll.printList();
    sll.addFirst("40");
    sll.printList();
    sll.insertSpecific("50",1);
    sll.printList();
    }
    }