Java Program to Create a Singly Linked List and Count the Number of Nodes Last Updated : 12 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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 = 5Operations: Create Node linked listDefine methods like addNode(),displayNodes() and countNodes()Get the final answer Implementation: Java // 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()); } } OutputNodes : 1 2 3 4 Total Nodes: 4Time Complexity For new node Insertion: At first: O(1)At End: O(N), where N is the size of linked listTime Complexity For count number of nodes: O(N), where N is a number of nodes Auxiliary space: O(1) as it is using constant space Comment More infoAdvertise with us Next Article Java Program to Create a Singly Linked List and Count the Number of Nodes N nspatilme Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read Java Program For Printing Nth Node From The End Of A Linked List Given a Linked List and a number n, write a function that returns the value at the n'th node from the end of the Linked List.For example, if the input is below the list and n = 3, then the output is "B". Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Method 1 (Use 5 min read Java Program to Insert a New Node at the Beginning of the Circular Linked List Circular linked list: A circular linked list is a sequence of elements in which every element points to its next element in the sequence and the last element has a link to the first element. That means a circular linked list is similar to the single linked list except that the last node points to th 4 min read Java Program to Get the First and the Last Element of a Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The given task is to retrieve the first and the last element of a given linked list. Properties of a Linked ListElements are stored in a non-contiguous manner.Every element is an object whi 4 min read Java Program to Insert a New Node at the Middle of the Circular Linked List Given a Circular Linked List, the task is to add a New Node at the Middle of the List. Let's consider the following Circular Linked List: List-Before-InsertionList-After-InsertionCreate a new node (New_node).Check for an empty list. If the list is empty then insert the node as head.For non-empty lis 3 min read Java Program To Find The Sum Of Last N Nodes Of The Given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list. Examples: Input: 10->6->8->4->12, n = 2 Output: 16 Sum of last two nodes: 12 + 4 = 16 Input: 15->7->9->5->16->14, n = 4 10 min read Java Program to Delete a Node From the Beginning of the Circular Linked List In this article, we will learn about deleting a node from the beginning of a circular linked list. Consider the linked list as shown below. Example: Input : 5->3->4->(head node) Output: 3->4->(head node) Two cases arrive while solving the problem, Case 1: List is empty If the list is 3 min read Java Program For Removing Every K-th Node Of The Linked List Given a singly linked list, Your task is to remove every K-th node of the linked list. Assume that K is always less than or equal to length of Linked List.Examples : Input: 1->2->3->4->5->6->7->8 k = 3 Output: 1->2->4->5->7->8 As 3 is the k-th node after its deletion list would be 1->2->4->5->6->7-> 3 min read Java Program to Delete a Node From the Middle of the Circular Linked List In this article, we are going to learn to delete the middle node from the circular Linked List in java. The approach we are going to follow for this program is, first we calculate the number of nodes in the list and then divide the number of nodes by 2 to get the middle node of the list. Algorithm C 4 min read Java Program For Writing A Function To Get Nth Node In A Linked List Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before moving on to th 4 min read Like