分类: C/C++
2011-01-22 16:48:52
用递归实现单链表的反向
(1) --> (2) --> (3) --> (4) --> (5)
(1) <-- (2) <-- (3) <-- (4) <-- (5)
struct node {
int val;
struct node *next;
};
struct node *head;
struct node *reverse(struct node *node)
{
static struct node *thead;
struct node *tnode;
thead = node;
if (!node || !node->next)
return node;
tnode = reverse(node->next);
tnode->next = node;
if (head == node)
{
node->next = NULL;
return thead;
}
else
{
return node;
}
}