题目
计算单链表的长度
解答
可使用while循环遍历链表,也可以使用递归遍历链表
解法一
int length(linklist l)
{
lnode *p = l->next;
int length = 0;
while (p)
{
length++;
p = p->next;
}
return length;
}
解法二
int length(linklist l)
{
static int i = 0;
if (l->next == nullptr)
{
return i;
}
i++;
length2(l->next);
}