Chinaunix首页 | 论坛 | 博客
  • 博客访问: 134184
  • 博文数量: 37
  • 博客积分: 2671
  • 博客等级: 少校
  • 技术积分: 335
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-11 13:18
文章分类

全部博文(37)

文章存档

2011年(16)

2010年(21)

我的朋友

分类: 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;
    }

}

阅读(405) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~