【发布时间】:2014-06-03 14:29:26
【问题描述】:
我正在尝试使用递归和下面的代码来反转链表
struct node* reverse_list(struct node *head)
{
struct node *temp=head,*rhead,*first;
if (temp->next== NULL)
return temp;
else
{
first = head;
rhead = reverse_list(temp->next);
first->next = NULL;
rhead->next = first;
return rhead;
}
}
我找不到任何错误,但我仍然没有得到正确的输出。请帮助。
【问题讨论】:
-
-- 已删除 -- 抱歉,我的错误 另一个提示:告诉我们您的输出是什么,这样我们也更容易看到问题所在
-
@Pinna_be 该问题未标记
C++。因此struct node不等于node。 -
@Pinna_be:如果没有
typedef,则结构类型为struct tag。您可以在 C++ 中省略struct关键字,但在 C 中则不能。 -
为什么要使用递归的方式?它基本上是将所有列表元素的指针多次复制到堆栈上。问题很简单,只需在修改引用时迭代列表即可。
-
@Veger:我已经有了使用迭代的解决方案,我正在尝试使用递归的解决方案
标签: c linked-list