本题要求,将给定单向链表逆置,即表头置为表尾,表尾置为表头。
链表结点定义如下:
struct ListNode {
int data;
struct ListNode *next;
};
函数接口定义:
在这里描述函数接口。例如:
struct ListNode *create();
struct ListNode *inverse(struct ListNode *head); //链表的逆置
其中head是用户传入的链表的头指针;函数reverse将链表head逆置,并返回结果链表的头指针。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *create();
struct ListNode *inverse(struct ListNode *head); //链表的逆置
int main()
{
struct ListNode *p, *head = NULL;
head = create();
if(head==NULL)
printf("no data!");
else
{
head=inverse(head);
for ( p = head; p != NULL; p = p->next )
printf("%d ", p->data);
}
printf("\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例1:
1 2 3 4 5 6 -1
输出样例1:
6 5 4 3 2 1
输入样例2:
-1
输出样例2:
no data!
struct ListNode *create()
{
struct ListNode *p;
struct ListNode *o;
struct ListNode *i;
int a;
scanf("%d",&a);
o=NULL;
while(a!=(-1))
{
p=(struct ListNode *)malloc(sizeof(struct ListNode));
p->data=a;
p->next=o;
o=p;
scanf("%d",&a);
}
return(o);
}
struct ListNode *inverse(struct ListNode *head)
{
return(head);
}