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.

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:
- Beginning.
- At End.
- At nth position.
Space and Time Complexity Singly Linked List in Java:
Insertion Type | Time Complexity | Space Complexity |
---|---|---|
At the beginning | O(1) | O(1) |
At the end | O(n) | O(1) |
At a specific index | O(n) | O(1) |
2. Time complexity depends on how far we have to traverse the list.


Definition of Singly linked list in Java

// Node Class class Node{ int data; Node next; Node(int x) // parameterized constructor { data = x; next = null; } };
Method 1: Calls functions using object.
Method 2: Calls static functions from main.
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:
- Create a new node with given data.
- Set newNode.next = head.
- Update head = newNode.

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.
Idea: Traverse to the last node and link the new node to it.
Steps:
- Create a new node with given data.
- If list is empty, update head to new node.
- Else, traverse till lastNode.next == null.
- Set lastNode.next = newNode.

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.
Idea: Traverse to the (index – 1)th node and insert the new node after it.
Steps:
- Create a new node.
- Traverse the list to reach (index – 1)th node.
- Set newNode.next = current.next.
- Set current.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; } }
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 Linked List in Java
Code 1: (Object Oriented Approach)
- Uses a LinkedList class to manage the linked list.
- All insertion and utility methods are instance methods inside the class.
- Creates a list, inserts nodes at the beginning, end, and a specific position.
- Useful when you want to manage the list using object oriented structure.
Code 2: (Functional/Static Approach)
- Uses static methods and a separate Node class.
- Linked list is managed using the head pointer passed into methods.
- Performs same operations: insert at beginning, end, and after a given position.
- 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:
Runimport java.lang.*; class LinkedList { Node head; // Node Class class Node { int data; Node next; Node(int x) { data = x; next = null; } } // Insert at beginning public Node insertBeginning(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; return head; } // Insert at end 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; } // Insert after nth position public void insertAfter(int n, int data) { int size = calcSize(head); if (n < 1 || n > size) { System.out.println("Can't insert, invalid position."); return; } Node newNode = new Node(data); Node temp = head; while (--n > 0) temp = temp.next; newNode.next = temp.next; temp.next = newNode; } // Display linked list public void display() { Node node = head; while (node != null) { System.out.print(node.data + " "); node = node.next; } System.out.println(); } // Calculate size public int calcSize(Node node) { int size = 0; while (node != null) { node = node.next; size++; } return size; } } public class Main { 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
Runimport java.lang.*; class Node { int data; Node next; Node(int x) { data = x; next = null; } } public class Main { // Insert at beginning static Node insertBeginning(Node head, int data) { Node newNode = new Node(data); newNode.next = head; return newNode; } // Insert at end static Node insertEnd(Node head, int data) { Node newNode = new Node(data); if (head == null) return newNode; Node temp = head; while (temp.next != null) temp = temp.next; temp.next = newNode; return head; } // Insert after nth position static Node insertAfter(int pos, int data, Node head) { int size = calcSize(head); if (pos < 1 || pos > size) { System.out.println("Can't insert, invalid position."); return head; } Node newNode = new Node(data); Node temp = head; while (--pos > 0) temp = temp.next; newNode.next = temp.next; temp.next = newNode; return head; } // Display list static void display(Node node) { while (node != null) { System.out.print(node.data + " "); node = node.next; } System.out.println(); } // Calculate size 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); 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
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
Login/Signup to comment