代码:
#include "stdafx.h" #include<windows.h> #include<iostream> #include<vector> using namespace std; struct list_node { int value; list_node *next; }; list_node *init_list(int len) { list_node *head, *p, *q; p = (list_node *)malloc(sizeof(list_node)); p->value = 0; head = p; for (int i = 1; i < len; i++) { q = (list_node *)malloc(sizeof(list_node)); q->value = 2 * i; p->next = q; p = q; } p->next = NULL; return head; } int show_list(list_node *head) { if (head == NULL) return 0; list_node *p = head; while (p) { printf("%4d", p->value); p = p->next; } printf(" "); return 0; } list_node *reverse_list(list_node *head) { struct list_node *prev = NULL; struct list_node *current = head; while (current) { struct list_node *node = current->next; current->next = prev; prev = current; current = node; } return prev; } int main() { list_node *head = init_list(10); show_list(head); head = reverse_list(head); show_list(head); system("pause"); return 0; }