我正在查看斯坦福图书馆的以下代码:
void recursiveReverse(struct node** head_ref)
{
struct node* first;
struct node* rest;
/* empty list */
if (*head_ref == NULL)
return;
/* suppose first = {1, 2, 3}, rest = {2, 3} */
first = *head_ref;
rest = first->next;
/* List has only one node */
if (rest == NULL)
return;
/* put the first element on the end of the list */
recursiveReverse(&rest);
first->next->next = first;
/* tricky step -- see the diagram */
first->next = NULL;
/* fix the head pointer */
*head_ref = rest;
}
我不明白的是在最后一个递归步骤中,例如如果 list 是 1-2-3-4 现在最后一个递归步骤首先是 1,rest 是 2。所以如果你设置 *head_ref = rest ..这使得列表的头部 2 ?? 有人可以解释一下如何将列表的头部反转为 4 吗?