Java program to create a singly linked list of n nodes and count the number of nodesIn this program, we need to create a singly linked list and count the nodes present in the list. To accomplish this task, traverse through the list using node current which initially points to head. Increment current in such a way that current will point to its next node in each iteration and increment variable count by 1. In the end, the count will hold the value which denotes the number of nodes present in the list. Algorithm
a. countNodes() will count the nodes present in the list:
a. display() will display the nodes present in the list:
Program:Output: Nodes of the singly linked list: 1 2 3 4 Count of nodes present in the list: 4 Next TopicJava Programs |
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