typedef struct _node {
int val;
struct _node *next;
}NODE;
typedef struct _list {
NODE * head;
int count;
}LIST;
/***********************************************************
**逆序过程如下:
** head -> node1 -> node2 -> node3 -> node4 -> null
**step1 head -> node1 -> null node2 -> node3 -> node4 -> null
**step2 head -> node2 -> node1 -> null node3 -> node4 -> null
**step3 head -> node3 -> node2 -> node1 -> null node4 -> null
**step4 head -> node4 -> node3 -> node2 -> node1 -> null
************************************************************/
int reverse_list(LIST *list) {
if ((list == NULL) || (list->head == NULL))
return -1;
NODE *tmp1 = list->head->next;
NODE *tmp2 = NULL;
NODE *tmp3 = tmp1->next;
/*第一个结点会成为最后一个结点,next必须指向null*/
tmp1->next = NULL;
while (tmp3->next != NULL) {
tmp2 = tmp3;
tmp3 = tmp3->next;
tmp2->next = tmp1;
tmp1 = tmp2;
}
/*这里是step4*/
tmp3->next = tmp1;
list->head->next = tmp3;
return 0;
}