Find length of Linked List in java

If you want to practice data structure and algorithm programs, you can go through data structure and algorithm programs.
In this post , we will see how to find length of Linked List in java.
You can obviously use size() method of java Linked List class but here we are going to see how to find length of Linked List when you implement Linked List yourself.
There are two ways to find length of linked list:
  • Iterative
  • Recursion

Iterative:

You can iterate over next nodes until node is null and use counter to count number of iteration and you will get length of linked list in the end.
// Find length of linked list using iterative method
public int lengthOfLinkedList()
 {
  Node temp=head;
  int count = 0;
  while(temp!=null)
  {
   temp=temp.next;
   count++;  
  }
  return count;

Recursion: 

You can use recursion also to find length of linked list. Base case: If node is null , return 0. This will be base case of our iteration.
// Find length of linked list using recursion
public int lengthOfLinkedList()
 {
  Node temp=head;
  int count = 0;
  while(temp!=null)
  {
   temp=temp.next;
   count++;  
  }
  return count;
Java program to find length of LinkedList: Create a java file named SinglyLinkedList.java.
package org.arpit.java2blog;

class Node {
 public int data;
 public Node next;

 public void displayNodeData() {
  System.out.println("{ " + data + " } ");
 }
}

public class SinglyLinkedList {
 private Node head;
 
 public boolean isEmpty() {
  return (head == null);
 }

 // used to insert a node at the start of linked list
 public void insertFirst(int data) {
  Node newNode = new Node();
  newNode.data = data;
  newNode.next = head;
  head = newNode;
 }

// Find length of linked list using iterative method
public int lengthOfLinkedList()
{
   Node temp=head;
   int count = 0;
   while(temp!=null)
   {
  temp=temp.next;
  count++;  
   }
    return count;
}
// Find length of linked list using recursion 
public int lengthOfLinkedListRec(Node head)
 {
   Node temp=head;
   if(temp==null)
   {
      return 0;
    }
   else
   {
      return 1+ lengthOfLinkedListRec(temp.next);
    }
}
 // used to delete node from start of linked list
 public Node deleteFirst() {
  Node temp = head;
  head = head.next;
  return temp;
 }

 // used to delete node from start of linked list
 public Node deleteFirst(Node node) {
  Node temp = head;
  head = head.next;
  return temp;
 }

 // Use to delete node after particular node
 public void deleteAfter(Node after) {
  Node temp = head;
  while (temp.next != null && temp.data != after.data) {
   temp = temp.next;
  }
  if (temp.next != null)
   temp.next = temp.next.next;
 }
 
 // used to insert a node at the start of linked list
 public void insertLast(int data) {
  Node current = head;
  while (current.next != null) {
   current = current.next; // we'll loop until current.next is null
  }
  Node newNode = new Node();
  newNode.data = data;
  current.next = newNode;
 }
 
 // For printing Linked List
 public void printLinkedList() {
  System.out.println("Printing LinkedList (head --> last) ");
  Node current = head;
  while (current != null) {
   current.displayNodeData();
   current = current.next;
  }
  System.out.println();
 }
}
Lets create Main class named LinkedListMain.java to create LinkedList.
package org.arpit.java2blog;

public class LinkedListMain {

 public static void main(String args[])
 {
   SinglyLinkedList myLinkedlist = new SinglyLinkedList();
   myLinkedlist.insertFirst(5);
   myLinkedlist.insertFirst(6);
   myLinkedlist.insertFirst(7);
   myLinkedlist.insertFirst(1);
   myLinkedlist.insertLast(2);
   myLinkedlist.printLinkedList();
   // Linked list will be 
   // 2 -> 1 ->  7 -> 6 -> 5
   System.out.println("Length of Linked List using iteration: "+myLinkedlist.lengthOfLinkedList());
   System.out.println("Length of Linked List Using recursion: "+myLinkedlist.lengthOfLinkedListRec(myLinkedlist.getHead()));
 }
}


When you run above program, you will get below output:
Printing LinkedList (head --> last) 
{ 1 } 
{ 7 } 
{ 6 } 
{ 5 } 
{ 2 } 
Length of Linked List using iteration: 5
Length of Linked List Using recursion: 5

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012