#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist() { struct ListNode *head, *p, *q; int num; head = p = (struct ListNode *)malloc(sizeof(struct ListNode)); scanf("%d", &num); while (num != -1) { p->data = num; q = (struct ListNode *)malloc(sizeof(struct ListNode)); p->next = q; p = q; scanf("%d", &num); } p->next = NULL; return head; } struct ListNode *reverse(struct ListNode *head) { struct ListNode *pre, *cur, *next; pre = NULL; cur = head; while (cur) { next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; } void printlist(struct ListNode *head) { struct ListNode *p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { struct ListNode *head; head = createlist(); head = reverse(head); printlist(head); return 0; }

C语言单向链表逆置算法详解及实现