首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

带表头的单链表递归实现逆转方法

2013-10-01 
带表头的单链表递归实现逆转方法求助无表头的单链表用递归非递归实现逆转都较容易实现,对于有表头的单链表

带表头的单链表递归实现逆转方法求助
无表头的单链表用递归非递归实现逆转都较容易实现,对于有表头的单链表非递归可以容易实现逆转,如果用递归有没有较好的办法,求大神指点菜鸟~
[解决办法]
这样?


node *reverse_list(node *head, node *pre = NULL)
{
    if (NULL == head)
        return pre;
    node *new_head = reverse_list(head->next, head);
    head->next = pre;
    return new_head;
}

[解决办法]
to 7L:
是的,一个参数就行了

node *reverse_list(node *head)
{
    if (NULL == head 
[解决办法]
 NULL == head->next)
        return head;
    node *new_head = reverse_list(head->next);
    head->next->next = head;
    head->next = NULL;
    return new_head;
}

热点排行