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

Join The Discussion