Java Program to insert a new node at the middle of the singly linked list17 Mar 2025 | 4 min read In this program, we will create a singly linked list and add a new node at the middle of the list. To accomplish this task, we will calculate the size of the list and divide it by 2 to get the mid-point of the list where the new node needs to be inserted. ![]() Consider the above diagram; node 1 represents the head of the original list. Let node New is the new node which needs to be added at the middle of the list. First, we calculate size which in this case is 4. So, to get the mid-point, we divide it by 2 and store it in a variable count. Node current will point to head. First, we iterate through the list till current points to the mid position. Define another node temp which point to node next to current. Insert the New node between current and temp Algorithm
a. addInMid() will add a new node at the middle of the list:
a. display() will display the nodes present in the list:
Program:Output: Original list: 1 2 Updated List: 1 3 2 Updated List: 1 3 4 2 Next TopicJava Programs |
In this program, we need to check whether a given string is palindrome or not. A string is said to be palindrome if it is the same from both the ends. For e.g. above string is a palindrome because if we try to read it from...
2 min read
? Two strings are called anagrams if they contain same set of characters but in different order. "keep ? peek", "Mother In Law - Hitler Woman". File: AnagramString .java import java.util.Arrays; public class AnagramString { static void isAnagram(String str1, String str2) { String s1 = str1.replaceAll("\\s", ""); String s2 = str2.replaceAll("\\s", ""); boolean...
1 min read
In this program, we will find out the smallest node in the given binary tree. We first define variable min that will hold root's data. Then, we traverse through left sub-tree to find the smallest node in left subtree. Compare it with min and store...
5 min read
In this section, we will discuss what is Armstrong number and also create Java programs to check if the given number is an Armstrong number or not. The Armstrong number program frequently asked in Java coding interviews and academics. Armstrong Number An Armstrong number is a positive...
5 min read
5432* 543*1 54*21 5*321 *4321 Algorithm: STEP 1: START STEP 2: DEFINE i, j. STEP 3: SET lines=5 STEP 4: SET i=1. REPEAT STEP 5 to STEP 10 UNTIL i <= lines STEP 5: SET j=lines STEP 6: REPEAT STEP 7 and 8 UNTIL j >= 1 STEP 7: IF j!=i then PRINT j ...
1 min read
Java program to print the following pattern *000*000* 0*00*00*0 00*0*0*00 000***000 Algorithm: STEP 1: START STEP 2: SET lines=4 STEP 3: Define i, j STEP 4: SET i =1 STEP 5: REPEAT STEP 6 to 15 UNTIL i <= lines STEP 6: SET j=1 STEP 7: REPET STEP 8 and 9 UNTIL j <= lines STEP 8: IF...
2 min read
An array containing various numbers is given. The task is to create different groups, each containing only two elements, such that the difference between the group containing the largest sum and the group containing the lowest sum should be minimum. Note that any element can only...
5 min read
Java Convert boolean to String We can convert boolean to String in java using String.valueOf(boolean) method. Alternatively, we can use Boolean.toString(boolean) method which also converts boolean into String. 1) String.valueOf() The String.valueOf() method converts boolean to String. The valueOf() is the static method of String class. The signature of...
1 min read
In this program, we need to display the upper triangular matrix. Upper Triangular Matrix Upper triangular matrix is a square matrix in which all the elements below the principle diagonal are zero. To find the upper triangular matrix, a matrix needs to be a square matrix that...
4 min read
In this program, we need to check whether two trees are identical or not. For two trees to be identical, they have to satisfy two conditions: Structure of both the trees should be the same. Nodes present in one tree should be present in another tree. Above diagram...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India