answersLogoWhite

0


Best Answer

int numNodes = 0;

Node current = root;

// if root is null, the number of nodes is 0

if(current != null) {

// if root is not null, we have at least one node

numNodes = 1;

// count all nodes

while(current .next != null) {

++numNodes;

current = current.next;

}

}

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the algorithm to count no of nodes in singly linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


What is the time complexity of Dijkstra's algorithm?

Dijkstra's original algorithm (published in 1959) has a time-complexity of O(N*N), where N is the number of nodes.


What is node class?

When creating linked lists (singly- or doubly-linked), the elements in the list are known as nodes. A node class defines how these nodes work. Generally, a node will contain a member pointer to the data it represents (which is typically an abstract data type rather than a concrete data class), as well as a pointer to the next node in the list, and the previous node if it is doubly-linked. The list itself maintains a member pointer to the head node in the list, and the tail node if doubly-linked. In this way, the list is only concerned with the nodes, not the data within those nodes. By the same token the nodes are only concerned with their nearest neighbouring nodes, not the list, nor the data they contain. Similarly, the data is only concerned with itself and other data, it is not concerned with the fact it may be part of a list or a node. All work concerning nodes can then be delegated to the nodes themselves, while any work relating to the data can be delegated to the data. Thus each class plays a small but clearly defined role within the list; no class does any more than it has to, which greatly simplifies the list interface.


What is Dijkstra's algorithm?

Dijkstra's algorithm is used by the OSPF and the IS-IS routing protocols. The last three letters in OSPF (SPF) mean "shortest path first", which is an alternative name for Dijkstra's algorithm.


What is the complexity of kruskal's minimum spanning tree algorithm on a graph with n nodes and e edges?

o(eloge)

Related questions

What is the algorithm to create and count the number of nodes in a single linked list?

Traverse the nodes from the beginning to the end, counting the nodes as you go. This takes linear time O(n) to count n nodes. A more efficient approach that enables constant-time O(1) counting is to encapsulate the list along with a node counter. Increment the counter each time a node is inserted and decrement when a node is extracted.


What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


How do you test a list to see if it is empty in C?

There are two types of list: singly-linked and doubly-linked. A singly-linked list is more commonly referred to as a forward list because it only allows forward traversal from the head node. When we refer to a list we usually mean a doubly-linked list, which allows bi-directional traversal from the head or the tail. A list generally maintains a count of its elements so if the count is zero, the list must be empty. This is a constant time operation. Forward lists are more compact than lists and are primarily intended for short lists. To improve efficiency, a forward list generally does not maintain a count of its elements. If we need a count, then we have to traverse the list, counting nodes as we go. However, we don't actually need to count the elements to test if a list is empty, we can simply examine the head node pointer. If it is NULL, there are no data elements so the list must be empty. Again, this is a constant time operation.


What is the Algorithm to count the number of leaf nodes in binary tree?

int countleaves(struct node* root){ if(root!=null) { countleaves(root->left); if(root->left==NULL&&root->right==NULL) { count++; } countleaves(root->right); } }


How are nodes linked?

That depends on the linking rules.


What is multiple link?

A (singly) linked list is a data structure of nodes where each node contains a value and a pointer to the next node.A doubly linked list is a data structure where each node has two pointers, one to the next and one to the previous node, so you can traverse the chain of links in both directions.A multiple linked list has several pointers in each node. They are used to traverse the nodes in different orders, for example one set of links ordered by date of birth and another ordered alphabetically by name.


What is the time complexity of Dijkstra's algorithm?

Dijkstra's original algorithm (published in 1959) has a time-complexity of O(N*N), where N is the number of nodes.


What are the differences between singly-linked doubly-linked and circularly-linked lists?

The difference is how many pointers each node has, and what they are pointing to. A linked list is comprised of "Nodes" each node contains data as well as 1 or more pointers. A singly linked list has one pointer per node, and a doubly linked list has 2 pointers per node. Some programs use several pointers per node. The purpose of these pointers is to hold the list together. In a singly linked list, you can view a node and can then move on to the next node that it is pointing to until you've passed through them all. A doubly-linked list would have a pointer to the next node as well as to the previous node. Thus you can move forward and backward through the list. A circularly-linked list doesn't necessarily have a set number of pointers because it simply means that the last node points to the first node creating a big circle. A non-circularly-linked list would not contain this last to first pointer and thus you would eventually reach the end of the list and stop.


How do you implement a doubly linked list by using singly linked list?

To implement a doubly linked list using a singly linked list, you can create two nodes in each element of the singly linked list - one for the next element and another for the previous element. This way, each node will have access to both its previous and next nodes, effectively creating a doubly linked list structure using a singly linked list implementation.


What is node class?

When creating linked lists (singly- or doubly-linked), the elements in the list are known as nodes. A node class defines how these nodes work. Generally, a node will contain a member pointer to the data it represents (which is typically an abstract data type rather than a concrete data class), as well as a pointer to the next node in the list, and the previous node if it is doubly-linked. The list itself maintains a member pointer to the head node in the list, and the tail node if doubly-linked. In this way, the list is only concerned with the nodes, not the data within those nodes. By the same token the nodes are only concerned with their nearest neighbouring nodes, not the list, nor the data they contain. Similarly, the data is only concerned with itself and other data, it is not concerned with the fact it may be part of a list or a node. All work concerning nodes can then be delegated to the nodes themselves, while any work relating to the data can be delegated to the data. Thus each class plays a small but clearly defined role within the list; no class does any more than it has to, which greatly simplifies the list interface.


What is Dijkstra's algorithm?

Dijkstra's algorithm is used by the OSPF and the IS-IS routing protocols. The last three letters in OSPF (SPF) mean "shortest path first", which is an alternative name for Dijkstra's algorithm.


What does pruning mean?

Pruning is an algorithm used to reduce the number of nodes that have to be evaluated to reach the optimal solution. Pruning works by blocking the evaluation of nodes whose leaf nodes would give worse results compared to the one that was previously examined. Alpha-Beta Pruning is one such example of a pruning algorithm.