Insertion at nth position in Singly Linked List in Java

Insertion at nth Position in JAVA

Let’s have a look at the program to Insert at nth position in linked list in Java.

JAVA Program for insertion at nth position in a Linked List

Implementation

For Insertion at nth position in singly Linked List 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.

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
Insertion at the nth node in Linked List in Java
Uses Objects to call functions
Run
import java.lang.*;

class LinkedList {
    Node head;

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

        Node(int x) // parameterized constructor
        {
            data = x;
            next = null;
        }
    }
    public Node insertStart(int data)
    {
        // Creating newNode memory & assigning data value
        Node newNode = new Node(data);
        // assigning this newNode's next as current head node
        newNode.next = head;

        // re-assigning head to this newNode
        head = newNode;

        System.out.println(newNode.data + " inserted");
        return head;
    }


    public void display()
    {
        System.out.println();
        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 class Main
{
    public static void main(String args[])
    {
        LinkedList ll = new LinkedList();

        ll.insertStart(5);
        ll.insertStart(4);
        ll.insertStart(3);
        ll.insertStart(2);
        ll.insertStart(1);

        ll.display();

    }
}

Output

5 inserted
4 inserted
3 inserted
2 inserted
1 inserted

1 2 3 4 5
Uses Static functions that are called from static main
Run
import java.lang.*;

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

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

class Main
{
    static Node insertStart(Node head, int data)
    {
        // Creating newNode memory & assigning data value
        Node freshNode = new Node(data);

        // assigning this newNode's next as current head node
        freshNode.next = head;
        // re-assigning head to this newNode
        head = freshNode;

        System.out.println(freshNode.data + " inserted");
        return head;
    }

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

    public static void main(String args[])
    {
        Node head = null;
        head = insertStart(head,5);
        head = insertStart(head,4);
        head = insertStart(head,3);
        head = insertStart(head,2);
        head = insertStart(head,1);

        display(head);

    }
}

Output

5 inserted
4 inserted
3 inserted
2 inserted
1 inserted

1 2 3 4 5

Code for Insertion in a Linked List at a Given Node

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