#include <stdio.h> #include <stdlib.h> #include <assert.h> struct listNode { int id; struct listNode *next; }; struct listNode* ReverseIteratively(struct listNode* head) { assert(head != NULL); struct listNode *ptr1 = head; struct listNode *ptr2 = head->next; while(ptr2 != NULL) { struct listNode *ptr3 = ptr2->next; ptr2->next = ptr1; ptr1 = ptr2; ptr2 = ptr3; } head->next = NULL; head = ptr1; return head; } struct listNode* Reverse(struct listNode* first, struct listNode* second) { //采用的是递归的思想 if (second == NULL) return first; struct listNode* ptr = second->next; second->next = first; return Reverse(second, ptr); } int main() { int i = 2; struct listNode *head = (struct listNode*)malloc(sizeof(struct listNode)); head->id = 1; head->next = NULL; struct listNode *current = head; while (i < 10) { struct listNode *p = (struct listNode*)malloc(sizeof(struct listNode)); p->id = i; p->next = NULL; current->next = p; current = p; i++; } struct listNode *t = head; while(t != NULL) { printf("%d ", t->id); t = t->next; } printf("/n"); t = Reverse(NULL, head); while(t != NULL) { printf("%d ", t->id); t = t->next; } printf("/n"); return 0; }