Examveda

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


This Question Belongs to Data Structure >> Introduction To Data Structures

Join The Discussion

Related Questions on Introduction to Data Structures