#include <stdio.h>
#include <stdlib.h>
typedef struct _listNode
{
int data;
struct _listNode *next;
} listNode;
listNode *reverse(listNode *list)
{
if(list == NULL || list->next == NULL) {
return list;
}
listNode *cur, *prv = NULL;
while(list) {
cur = list;
list = list->next;
cur->next = prv;
prv = cur;
}
return cur;
}
void show(listNode *list)
{
while(list) {
printf("%d -> ", list->data);
list = list->next;
}
printf("NULL\n");
return;
}
int main(int argc, char const *argv[])
{
listNode *head = NULL, *tail, *tmp;
int i;
for(i = 0; i < 5; i ++) {
tmp = (listNode *)malloc(sizeof(listNode));
tmp->data = i;
if ( head == NULL) {
head = tmp;
tail = tmp;
} else {
tail->next = tmp;
tail = tmp;
}
}
tail->next = NULL;
show(head);
listNode *new = reverse(head);
show(new);
return 0;
}