Introduction
Finding the length of a linked list involves counting the number of nodes present in the list. This operation is fundamental when you need to determine the size of the linked list for various algorithms or applications. This guide will walk you through writing a Java program that calculates the length of a singly linked list.
Problem Statement
Create a Java program that:
- Implements a singly linked list.
- Calculates and displays the length of the linked list.
Example:
-
Input:
1 -> 2 -> 3 -> 4 -> 5 -
Output:
Length of the linked list is 5 -
Input:
10 -> 20 -> 30 -
Output:
Length of the linked list is 3
Solution Steps
- Create the Linked List and Node Structure: Define a
Nodeclass to represent each element in the linked list and aLinkedListclass to manage the list. - Add Nodes to the Linked List: Implement methods to add nodes to the linked list.
- Calculate the Length of the Linked List:
- Traverse the linked list from the head to the end.
- Count the number of nodes during the traversal.
- Display the Result: Output the length of the linked list.
Java Program
// Java Program to Find the Length of a Linked List
// Author: https://www.rameshfadatare.com/
class Node {
int data;
Node next;
// Constructor to initialize the node
public Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
// Method to add a new node at the end of the list
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
// Step 1: Initialize the head if the list is empty
head = newNode;
} else {
// Step 2: Traverse to the end of the list and add the new node
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
// Method to calculate the length of the linked list
public int length() {
int count = 0;
Node current = head;
// Step 3: Traverse the list and count the nodes
while (current != null) {
count++;
current = current.next;
}
return count;
}
// Method to display the linked list
public void display() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
}
public class LinkedListLength {
public static void main(String[] args) {
LinkedList list = new LinkedList();
// Adding elements to the linked list
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
System.out.println("Linked List:");
list.display();
// Finding the length of the linked list
int length = list.length();
System.out.println("Length of the linked list is: " + length);
}
}
Explanation
Step 1: Initialize the Node Class
- The
Nodeclass represents a single node in the linked list. Each node containsdataand a reference to thenextnode in the list. - The constructor initializes the node with data and sets the
nextpointer tonull.
Step 2: Initialize the LinkedList Class
- The
LinkedListclass manages the linked list. The class contains theheadnode that points to the first node in the list. - The
add()method appends a new node to the end of the list. If the list is empty, theheadnode is set to the new node.
Step 3: Traverse the List and Count the Nodes
- The
length()method calculates the length of the linked list by traversing from theheadto the last node. - A counter variable
countis initialized to 0 and is incremented for each node encountered during the traversal.
Output Example
Linked List:
1 -> 2 -> 3 -> 4 -> 5 -> null
Length of the linked list is: 5
Example with Different Input
If you modify the input list to:
list.add(10);
list.add(20);
list.add(30);
The output will be:
Linked List:
10 -> 20 -> 30 -> null
Length of the linked list is: 3
Conclusion
This Java program demonstrates how to find the length of a singly linked list by traversing the list and counting the nodes. The program efficiently calculates the size of the linked list, providing a fundamental operation that is useful in various applications and algorithms in Java programming.