Home >>Java Programs >Inserting a new Node at the middle of a LinkeList in Java

Inserting a new Node at the middle of a LinkeList in Java

Java Program to insert a new node at the middle of the singly linked list

In this Example, we will create a java program of singly linked list and add a new node at the middle of the list.

Program:

public class Main {  

class Node{  
int data;  
Node next;  

public Node(int data) {  
this.data = data;  
this.next = null;  
}  
}  

public int size;  
public Node head = null;  
public Node tail = null;  

public void addNode(int data) {  
Node newNode = new Node(data);  

if(head == null) {  
head = newNode;  
tail = newNode;  
}  
else {  
tail.next = newNode;  
tail = newNode;  
}  
size++;  
}  

public void addInMid(int data){  
Node newNode = new Node(data);  

if(head == null) {  
head = newNode;  
tail = newNode;  
}  
else {  
Node temp, current;  
int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);  
temp = head;  
current = null;  

for(int i = 0; i < count; i++) {  
    current = temp;  
    temp = temp.next;  
}  

current.next = newNode;  
newNode.next = temp;  
}  
size++;  
}  

public void display() {  
Node current = head;  
if(head == null) {  
System.out.println("List is empty");  
return;  
}  

while(current != null) {  
System.out.print(current.data + " ");  
current = current.next;  
}  
System.out.println();  
}  

public static void main(String[] args) {  

Main sList = new Main();  

sList.addNode(1);  
sList.addNode(2);  

System.out.println("Original list: ");  
sList.display();  

sList.addInMid(3);  
System.out.println( "Updated List: ");  
sList.display();  

sList.addInMid(4);  
System.out.println("Updated List: ");  
sList.display();  
}  
}  

Output:-
Original list:
1 2
Updated List:
1 3 2
Updated List:
1 3 4 2

Java Programs Check Palindrome Number in Java Factorial Program using loop in java Factorial Program using recursion in java Fibonacci Series Program in Java using recursion Fibonacci series without using recursion in Java Find an Armstrong Number in Java Prime Number Program in Java Find Prime numbers between two numbers in Java Break statement in Java for each loop in Java Typecasting in Java Printing the format text with printf in Java How to generate random numbers within a range in Java Java Program to count all punctuation characters in the string Java program to print the following given pattern Java program to print the following given pattern Java program to print the following given pattern Java program to print the following pattern Java program to print the following pattern Java program to print the given pattern Java program to print the given pattern Java program to print the given pattern Java program to print the following pattern Java program to print the following pattern Java Program to Print the following Pattern Java program to print the following pattern Java Program to Print the following Pattern Java Program to print the smallest element in an array Java Program to Print the following pattern Java Program to Print the following pattern Java Program to Print the following Pattern Java Program to Copy One Array to Another in Java Java Program to find the frequency of each element in a array Java Program to left rotate the elements of an array Java Program to print the duplicate elements of an array Java Program to print the elements of an array Java Program for binary search Java Program for linear search Java Program for bubble sort Java Program for insertion sort Java Program for selection sort Java Program to print the elements of an array present on even position Java Program to print the elements of an array in reverse order Java Program to find Third Largest Number in an Array Java Program to print the largest element in an array Print the number of elements in an array java Java Program to print the sum of all the items of the array Java Program to right rotate the elements of an array Java Program to sort the elements of an array in ascending order Java Program to sort the elements of an array in descending order Java Program to print the elements of an array present on odd position Java Program to Check if it is a Sparse Matrix Java Program to check a given matrix is an identity matrix Java Program to determine whether two matrices are equal Java Program to display the lower triangular matrix Java Program to find the product of two matrices
No Sidebar ads