#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;
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交