Table of Contents
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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())); } } |
1 2 3 4 5 6 7 8 9 10 |
Printing LinkedList (head --> last) { 1 } { 7 } { 6 } { 5 } { 2 } Length of Linked List using iteration: 5 Length of Linked List Using recursion: 5 |