首页 > 代码库 > 链表(4)----反转链表

链表(4)----反转链表

1、链表结构定义


typedef struct ListElement_t_ {
    void *data;
    struct ListElement_t_ *next;
} ListElement_t;

typedef struct List_t_{
    int size;
    int capacity;
    ListElement_t *head;
    ListElement_t *tail;
} List_t;



2、反转链表实现

int  ReverseList( List_t *list)
{
    if( list == NULL )
        return INPUT_ERROR;
    if( list->head == NULL )
        return INPUT_ERROR;    
    if( list->head->next == NULL )
        return 0;

    ListElement_t *pCurrent = list->head;
    ListElement_t *pNext = pCurrent->next;
    pCurrent->next = NULL;
    list->tail = pCurrent;
    while( pNext != NULL ){
        ListElement_t *pTmp = pNext->next;
        pNext->next = pCurrent;
        pCurrent = pNext;
        pNext = pTmp;
    }
    list->head = pCurrent;
    return 0;
}


链表(4)----反转链表