int last(Node *head) // complete this, must be recursive
{
Node *ptr = head;
if (ptr != NULL) {
if (ptr->next == NULL)
return ptr->num;
else
last(ptr->next);
}
}
我试图返回最后一个值,我觉得问题与我试图返回值的方式有关,但我不确定我应该怎么做。
答案 0 :(得分:2)
int last(Node *current)
{
// degenerate case
if ( current == NULL ) return 0; //or pick another number if you want
// last element found
if ( current->next == NULL ) return current->num;
// recursive
return last( current->next );
}