struct ListNode *reverse( struct ListNode *head )
{
if(head==NULL||head->next==NULL)//链表为空
return head;
struct ListNode *p,*q;
p=head->next;
head->next==NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=head->next;
head->next=q;
}
return head;
}
这是题中的代码
#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;
}
/* 你的代码将被嵌在这里 */