Hi. I need help with my size method. I am using a single-linked list. I tested my code and it returns 0 all the time.
/** This method returns the size of the current list. <br>
* If the current list has no elements,it is empty. <br>
*
* @return size of the current list*/
public int size() {
//A counter to keep track of the elements in the list
int listCounter = 0;
//initialize the first element as head
Node<T> node = head;
//if node is null, then the list is empty
//and an empty list has 0 elements
if (node == null) return 0;
//While the node is not on its last element,
while(node != null) {
//increment listCounter
listCounter ++;
//node becomes the next node
node = node.next;
}
//return counter after
return listCounter;
}