裁判测试程序样例
#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 *reverse( struct ListNode *head )
{
if(head==NULL) return head;
struct ListNode *p,*s;
//1.
p = head->next;
head->next = NULL;
while(p!=NULL)
{
//2.1
s = p;
p = p->next;
//2.2
s->next = head;
head = s;
}
return head;
}