Insertion in Linked List in Java

Singly Linked List Insertion in Java

In this article, we will explore Singly Linked List Insertion in Java in depth. We will discuss different insertion methods, their time and space complexities, detailed algorithms, and complete Java programs with example input and output.

Singly Linked Lists are one of the fundamental data structures in computer science. Unlike arrays, linked lists offer dynamic memory allocation, making them useful for situations where the size of the data set may change frequently.

Singly Linked List Insertion in Java Programming

What is Singly Linked List Insertion in Java?

A singly linked list is a linear data structure where each element (called a node) contains two parts:

  • Data: the actual value stored.
  • Next: a reference to the next node in the list.

Insertion in a singly linked list means adding a new node at:

  1. Beginning.
  2. At End.
  3. At nth position.

Space and Time Complexity Singly Linked List in Java:

Insertion TypeTime ComplexitySpace Complexity
At the beginningO(1)O(1)
At the endO(n)O(1)
At a specific indexO(n)O(1)
Singly-Linked-List-Insertion-in-Java
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;
    }
};

Prime Course Trailer

Related Banners

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

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.

Idea: Create a new node. Make its next point to the current head. Update head to the new node.

Steps:

  1. Create a new node with given data.
  2. Set newNode.next = head.
  3. Update head = newNode.
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.

Idea: Traverse to the last node and link the new node to it.

Steps:

  1. Create a new node with given data.
  2. If list is empty, update head to new node.
  3. Else, traverse till lastNode.next == null.
  4. Set lastNode.next = newNode.
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.

Idea: Traverse to the (index – 1)th node and insert the new node after it.

Steps:

  1. Create a new node.
  2. Traverse the list to reach (index – 1)th node.
  3. Set newNode.next = current.next.
  4. Set current.next = newNode.
Singly Linked List Insertion in Java 3

Full Code for Insertion in Linked List in Java

Code 1: (Object Oriented Approach)

  1. Uses a LinkedList class to manage the linked list.
  2. All insertion and utility methods are instance methods inside the class.
  3. Creates a list, inserts nodes at the beginning, end, and a specific position.
  4. Useful when you want to manage the list using object oriented structure.

Code 2: (Functional/Static Approach)

  1. Uses static methods and a separate Node class.
  2. Linked list is managed using the head pointer passed into methods.
  3. Performs same operations: insert at beginning, end, and after a given position.
  4. Useful when you want a modular or utility style implementation without creating a class for the list.

Let us have a look at the code below:

To wrap it up….

Insertion is a key operation when working with singly linked lists in Java.

  • Understanding how to insert at various positions helps build a strong foundation in data structures.
  • Whether you’re preparing for coding interviews or implementing real world software, knowing how to manipulate linked lists is crucial.

FAQ's related to Insertion in Linked List in Java

Answer:

You can insert a node at the beginning, at the end, or at a specific index in a singly linked list using simple pointer manipulation.

Answer:

  • Insertion at beginning: O(1)
  • Insertion at end or middle: O(n), where n is the number of nodes (due to traversal)

Answer:

Yes. If the list is empty (head is null), the new node becomes the head of the list.

Answer:

If the index is negative or beyond the length of the list, it’s considered invalid. The program should handle this with proper checks or error messages.

Answer:

Not always. Since we have to traverse to the last node, the time complexity is O(n). To improve efficiency, we can maintain a tail pointer for O(1) end insertions.

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