Java Program to create and display a singly linked list17 Mar 2025 | 3 min read The singly linked list is a linear data structure in which each element of the list contains a pointer which points to the next element in the list. Each element in the singly linked list is called a node. Each node has two components: data and a pointer next which points to the next node in the list. The first node of the list is called as head, and the last node of the list is called a tail. The last node of the list contains a pointer to the null. Each node in the list can be accessed linearly by traversing through the list from head to tail. ![]() Consider the above example; node 1 is the head of the list and node 4 is the tail of the list. Each node is connected in such a way that node 1 is pointing to node 2 which in turn pointing to node 3. Node 3 is again pointing to node 4. Node 4 is pointing to null as it is the last node of the list. Algorithm
a. display() will display the nodes present in the list:
Program:Output: Nodes of singly linked list: 1 2 3 4 Next TopicJava Programs |
An array or list inArr of the numbers (either negative or positive) is given. The task is to find the number of smaller elements that exist on the right side of the current element. The following examples are given for a better understanding. Example 1: Input int inArr[] =...
14 min read
Write a code to prove that strings are immutable in java? Following program demonstrate it. File: ProveStringImmutable .java public class ProveStringImmutable { public static void referenceCheck(Object x, Object y) { if (x == y) { System.out.println("Both pointing to the same reference"); } else { System.out.println("Both are pointing to different reference"); } } public static void...
1 min read
In this program, we will create a doubly linked list and delete a node from the end of the list. If the list is empty, print the message "List is empty". If the list is not empty, tail's ious node will become the new tail...
6 min read
In this section, we have created a number of Java program to check if a given number is perfect square or not. The perfect square or square number is a positive integer that is square of an integer. In other words, when we multiply two same numbers...
6 min read
. In this program, we need to find the reverse of the string. This can be done by iterating the string backward and storing each character from the original string into a new string. Original string: Dream big Reverse of the string: big maerD ALGORITHM STEP 1: START STEP 2: DEFINE...
2 min read
Problem Statement The problem states that a gardener wants to water a garden (single-dimensional) by opening the minimum number of taps. The task is to find the minimum number of taps that should be open to water the whole garden, return -1 if the garden cannot be...
17 min read
Java program to print the following pattern on the console 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Algorithm: STEP 1: START STEP 2: DEFINE i, j. STEP 3: SET n=5. STEP 4: SET i=n. REPEAT STEP 5 to STEP 7 UNTIL i>0. STEP 5: SET j=1. REPEAT STEP...
2 min read
ISBN is another special number in Java. ISBN stands for the International Standard Book Number that is carried by almost each every book. The ISBN is a ten-digit unique number. With the help of the ISBN, we can easily find any book. The ISBN number is...
3 min read
Program to print the largest element in an array In this program, we need to find out the largest element present in the array and display it. This can be accomplished by looping through the array from start to end by comparing max with all the...
2 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to solve...
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