链接列表查找长度 - 这两个函数之间的差异是什么?

Pra*_*han 0 c linked-list

这两个功能有什么区别吗?我的意思是返回的结果?

int Length(struct node* head) {
  struct node* current = head;
  int count = 0;

  while (current != NULL) {
    count++;
    current = current->next;
  }

  return count;
}
Run Code Online (Sandbox Code Playgroud)

和这个功能

int Length(struct node* head) {
  int count = 0;

  while (head != NULL) {
    count++;
    head = head->next;
  }

  return count;
}
Run Code Online (Sandbox Code Playgroud)

K-b*_*llo 6

他们是一样的.一个使用本地"当前"变量迭代列表,而另一个使用通过函数参数接收的相同变量.

  • 呵呵,它们在微观上是不同的"本地":"current"变量存在于函数*body*范围内,而`head`存在于*function*范围本身,但这两个范围之间的空间也是如此薄到一把刀. (3认同)