int function(struct node* head){ int var = 0; while(head->next != NULL){ var++; head = head->next; } return var; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

int function(struct node* head){ int var = 0; while(head->next != NULL){ var++; head = head->next; } return var; }

Can you find error in it

11th May 2022, 12:41 PM
Ravi Kiran
Ravi Kiran - avatar
2 Answers
0
Is "next" of type "pointer to node type"? If yes, are you sure the error isn't outside the function?
11th May 2022, 1:15 PM
OrHy3
OrHy3 - avatar
0
If this function counts elements in linked list, then you should change condition in loop on head != NULL, because your answer is always 1 less than actual length of linked list.
11th May 2022, 2:44 PM
Alexus100
Alexus100 - avatar