int List::size() {
// FIXME: implement this method to return the number of Nodes in the list
int listSize = 0;
Node* temp = first;
while (temp != nullptr) {
listSize++;
temp = temp->next;
}
return listSize;
}
I’m implementing this linked list class method to calculate the number of nodes in the list. When I execute the program, I return the correct number of nodes however I also get a segfault and I’m curious if there’s something wrong in this method.
New contributor
Garrett Morgan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.