数据结构练习(17)反转链表

http://zhedahht.blog.163.com/blog/static/2541117420073471124487/

链表操作的经典,简短的代码写出健壮的程序,这才是编程之美啊

struct ListNode {
    int value;
    ListNode *next;
};

ListNode * reverse_list(ListNode *head)
{
    ListNode *rvs = NULL;
    ListNode *pre = NULL;
    ListNode *node = head;

    while (node != NULL)
    {
        ListNode *next = node->next;

        if (next != NULL)
            rvs = node;

        node->next = pre;
        pre = node;
        node = next;
    }
    return rvs;
}

 

posted @ 2012-12-13 15:39  kedebug  阅读(161)  评论(0编辑  收藏  举报