原题链接++++++
#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()
{
struct ListNode *l=NULL,*r=NULL,*s;
int x;
while (scanf ("%d",&x),x!=-1)
{
s = (struct ListNode *) malloc(sizeof (struct ListNode));
s->data = x;
if (r)
{
r->next = s;
r = s;
}
else
l = r = s;
}
return l;
}
struct ListNode *reverse( struct ListNode *head )
{
struct ListNode *l = head,*r,*st=NULL;
while (l)
{
r = l->next;
if (st)
{
l->next = st;
st = l;
}
else
{
st = l;
st->next = NULL;
}
l = r;
}
return st;
}