In a lot of examples I’ve read a simple getListLength() function would look something like this:
int getListLength(struct node *head)
{
struct node *temp = head;
int iCount = 0;
while (temp)
{
++iCount;
temp = temp->next;
}
return iCount;
}
What strikes me as unneeded is the declaration of a local pointer (in this case *temp) that copies the passed parameter. If I recall correctly, passed parameters obtain their own copies. Thus, there won’t be a need for a local pointer that copies the *head just because the *head is a copy itself, right?
In other words, would it be correct to discard the *temp pointer and use head everywhere instead?
Yes, it’s a copy, so yes, it would be correct.
Why don’t you execute it and see for yourself?