Skip to content
Related Articles

Related Articles

Java Program to Create a Singly Linked List and Count the Number of Nodes
  • Last Updated : 26 Nov, 2020

Linked List is a linear data structure. Linked list elements are not stored at a contiguous location, the elements are linked using pointers. Singly Linked list is the collection of nodes, where each node has two parts one is the data and other is the linked part.

Example:

Input : AddNodes = {2, 3, 4}
Output: LinkedList = [2, 3, 4]
        Size = 3
        
Input : AddNodes = {1, 2, 3, 4, 5}
Output: LinkedList = [1, 2, 3, 4, 5]
        Size = 5

Operations:

  1. Create Node linked list
  2. Define methods like addNode(),displayNodes() and countNodes()
  3. Get the final anwer

 Implementation:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Create a Singly Linked List
// of n Nodes and Count the Number of Nodes
import java.io.*;
import java.util.*;
  
public class LinkedListCreation {
  
    class Node {
        int data;
        Node next;
  
        // constructor to create new node
        Node(int data)
        {
            this.data = data;
            this.next = null;
        }
    }
  
    // Initially both head and tail are not
    // pointing to any other node
    Node head = null;
    Node tail = null;
  
    // method to add newNode in Linked List
    void addNode(int data)
    {
  
        Node newNode = new Node(data);
  
        // Checks if the list is empty
        if (head == null) {
            // If list is empty, both head and
            // tail will point to new node
            head = newNode;
            tail = newNode;
        }
        else {
  
            tail.next = newNode;
            // storing newnode in tail
            tail = newNode;
        }
    }
    // display linked list
    void displayNodes()
    {
  
        Node current = head;
        if (head == null) {
            System.out.println("Empty");
            return;
        }
        System.out.println("Nodes : ");
        while (current != null) {
  
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
  
    // method to count nodes
    int countNodes()
    {
        // Initially zero
        int count = 0;
  
        Node currentNode = head;
        // iterate until all the nodes are present
        while (currentNode != null) {
  
            count++;
            currentNode = currentNode.next;
        }
        // return the count
        return count;
    }
  
    public static void main(String[] args)
    {
  
        LinkedListCreation L1 = new LinkedListCreation();
  
        L1.addNode(1);
        L1.addNode(2);
        L1.addNode(3);
        L1.addNode(4);
  
        // Displays the nodes present in the list
        L1.displayNodes();
  
        // Counts the nodes present in the given list
        System.out.println("Total Nodes: "
                           + L1.countNodes());
    }
}

chevron_right


Output



Nodes : 
1 2 3 4 
Total Nodes: 4

Time Complexity For new node Insertion:

  1. At first: O(1)
  2. At End: O(N), where N is the size of linked list

Time Complexity For count number of nodes: O(N), where N is a number of nodes

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :