Inserting into a singly linked list means creating a new node and rewiring one or two next references so the node becomes part of the chain. No existing element has to move; the work is in finding the right place.
This tutorial covers the four common insert positions:
- At the beginning of the linked list.
- At a specific position of the linked list.
- After a given node of the linked list.
- At the end of the linked list.
1. Insert a node at the beginning of the linked list
The new node simply goes in front of the current first node:
Create newNode holding the value.
Point newNode.next at the current head. If the list is empty, head is
null, so the new node'snextisnullas well.Make newNode the new head.
The same two assignments work for an empty and a non-empty list, and the operation takes O(1) time.
In the example below, we will see how to insert a new node at the beginning of the linked list.
// Java Program to insert a new node at the beginning of Linked List
public class InsertNodeAtBeginningInSLL {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
// method to insert a new node at the beginning of the linked list
static Node insertAtFront(Node head, int data) {
// Create a node with the given parameter data
Node node = new Node(data);
// Assigning next node reference from the head
node.next = head;
// Return the new node as the new head of the list
return node;
}
// method to print linked list
public static void printLinkedList(Node head) {
Node tempNode = head;
while (tempNode != null) {
System.out.print(" " + tempNode.data);
tempNode = tempNode.next;
}
System.out.println(); // Line break
}
public static void main(String[] args) {
// create a head node
Node head = new Node(40);
head.next = new Node(60);
head.next.next = new Node(80);
head.next.next.next = new Node(100);
// Print the current linked list data
System.out.println("Linked List values: ");
printLinkedList(head);
// Insert a new node at the beginning of the linked list
System.out.println("After inserting a Node at the beginning:");
head = insertAtFront(head, 20); // Inserting 20 at the beginning
// Print the linked list after inserting a new node at the beginning
printLinkedList(head);
}
}
Linked List values: 40 60 80 100 After inserting a Node at the beginning: 20 40 60 80 100
2. Insert a node at a specific position of the linked list
Positions are counted from 1 (the head). To place a node at position p:
Reject positions smaller than 1.
If p is 1, insert at the beginning as shown above.
Otherwise start a pointer current at head and move it p − 2 steps, so that it stops on the node that will come before the new one.
If current becomes
nullon the way, the list is too short and the position is out of bounds.Link the new node in: first newNode.next = current.next, then current.next = newNode.
Reaching the position takes O(p) steps, so this is O(n) in the worst case.
In the example below, we will see how to insert a new node at a specific position in the linked list.
// Java Program to Insert a Node At a Specific Position in Linked List
public class InsertNodeAtSpecificPositionInSLL {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
// Method to insert a new node at the specific position in
// the linked list
static Node insertAtSpecPosition(Node head, int position, int data) {
// Create a node
Node node = new Node(data);
// Positions start at 1
if (position < 1) {
System.out.println("ERROR: Position must be 1 or greater.");
return head;
}
// Position 1 means the new node becomes the head
if (position == 1) {
node.next = head;
head = node;
return head;
}
// If the position is other than 1
Node current = head;
for (int i = 1; i < position - 1 && current != null; ++i) {
current = current.next;
}
// If the position is out of bounds
if (current == null) {
System.out.println("ERROR: Position is out of bounds.");
return head;
}
node.next = current.next;
current.next = node;
return head;
}
// Print a linked list
static void printLinkedList(Node head) {
while (head != null) {
System.out.print(" " + head.data);
head = head.next;
}
System.out.println(); // New Line
}
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(40);
head.next.next.next = new Node(50);
System.out.print("Linked list before inserting a new node:\n");
printLinkedList(head);
// Insert a new node with data 30 at position 3
int data = 30, position = 3;
head = insertAtSpecPosition(head, position, data);
System.out.print("\nLinked list after inserting 30 at the position " + position + ":\n");
printLinkedList(head);
}
}
Linked list before inserting a new node: 10 20 40 50 Linked list after inserting 30 at the position 3: 10 20 30 40 50
3. Insert a node after a given node of the linked list
Here the caller names an existing value (the key) and the new node goes right after the first node holding it:
Walk from head with a pointer node until node.data equals the key or node becomes
null.If node is
null, the key is not in the list (this also covers an empty list), so nothing is inserted.Otherwise create newNode and link it in: newNode.next = node.next, then node.next = newNode.
Finding the key is O(n); the insertion itself is O(1).
In the example below, we will see how to insert a new node after a given node in the linked list.
// Java Program to Insert a Node after a Given Node in Linked List
public class InsertNodeAtAfterGivenNodeInSLL {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
// Method to insert a new Node after a given Node
static Node insertAfter(Node head, int key, int newData) {
Node node = head;
// Find the first node that holds the key
while (node != null) {
if (node.data == key)
break;
node = node.next;
}
// node is null when the key is not in the list
if (node == null)
return head;
// Link the new node in after the node that holds the key
Node newNode = new Node(newData);
newNode.next = node.next;
node.next = newNode;
return head;
}
static void printLinkedList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println(); // New Line
}
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(40);
head.next.next.next = new Node(50);
System.out.println("Linked List data: ");
printLinkedList(head);
int key = 20, newData = 30;
// Insert a new node 30, after the node 20
head = insertAfter(head, key, newData);
System.out.println("After insertion, Linked List data: ");
printLinkedList(head);
}
}
Linked List data: 10 20 40 50 After insertion, Linked List data: 10 20 30 40 50
4. Insert a node at the end of the linked list
Without a tail reference, the last node has to be found first:
Create newNode; its
nextisnullbecause it will be the last node.If the list is empty, newNode becomes the head.
Otherwise move a pointer last from head until last.next is
null.Set last.next = newNode.
The walk makes this O(n). Keeping a tail reference (see FAQ Q4) makes it O(1).
In the example below, we will see how to insert a new node at the end of the linked list.
// Java Program to Insert a Node at the End of Linked List
public class InsertNodeAtEndInSLL {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
// Method to insert a new node at the end of the linked list
static Node insertAtEnd(Node head, int data) {
// Create a new node
Node node = new Node(data);
// If the Linked List is empty, make the new
// node as the head and return
if (head == null) {
return node;
}
// Start from the head and look for the last node
Node last = head;
// Traverse till the last node of the linked list
while (last.next != null) {
last = last.next;
}
// Change the next pointer of the last node to point to the new node
last.next = node;
// Return the head of the list
return head;
}
// Method to print linked list
static void printLinkedList(Node node) {
while (node != null) {
System.out.print(" " + node.data);
node = node.next;
}
}
public static void main(String[] args) {
// Create a linked list
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
System.out.print("Linked list values: ");
printLinkedList(head);
// Inserting a node at the end of the linked list
head = insertAtEnd(head, 50);
System.out.print("\nLinked List values, after inserting at the end: ");
printLinkedList(head);
}
}
Linked list values: 10 20 30 40 Linked List values, after inserting at the end: 10 20 30 40 50