```c
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode* next;
};
struct ListNode* createlist();
struct ListNode* reverse(struct ListNode* head);
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;
}
struct ListNode* createlist(void)
{
struct ListNode* head = NULL, * tail = NULL, * p = NULL;
int data;
while (1) {
scanf("%d", &data);
if (data == -1)
return head;
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = data;
p->next = NULL;
if (head) {
tail->next = p;
tail = p;
}
else {
head = p;
tail = p;
}
}
}
struct ListNode* reverse(struct ListNode* head)
{
struct ListNode* p = head;
struct ListNode* newhead = NULL;
while (p)
{
struct ListNode* abc = (struct ListNode*)malloc(sizeof(struct ListNode));
abc->data = p->data; abc->next=NULL;
if (newhead == NULL)
newhead = abc;
else
{
abc->next = newhead;
newhead = abc;
}
p = p->next;
}
return newhead;
}