【发布时间】:2016-07-19 17:37:37
【问题描述】:
我是链表的新手。我正在尝试编写可以将链接列表复制到新列表的 CopyList() 代码。有一个使用递归的独特版本,我不太明白:
struct node
{
int data;
struct node *next;
};
struct node* CopyList(struct node* head) {
struct node* current = head;
if (current == NULL) return NULL;
else {
struct node* newList = malloc(sizeof(struct node));
newList->data = current->data;
newList->next = CopyList(current->next); // recur for the rest
return(newList);
}
}
我理解的麻烦是 newList->next = CopyList(current->next); 那么这对复制有什么作用?为什么?
【问题讨论】:
-
第一!将电流改为头部。 current 在任何地方都没有定义。
标签: recursion linked-list