Which of the following piece of code has the functionality of counting the number of elements in the list?
A.
public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
size++;
cur = cur.getNext();
}
return size;
}B.
public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
cur = cur.getNext();
size++;
}
return size;
}C.
public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
size++;
cur = cur.getNext();
}
}D.
public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
size++;
cur = cur.getNext().getNext();
}
return size;
}Answer: Option A
Related Questions on Introduction to Data Structures
A. A collection of data values
B. A programming language
C. A set of algorithms
D. A way of organizing and storing data

Join The Discussion