题目(2016亚信实习生笔试题):
将链表后半部分反转,编写功能函数。函数头类似于本代码中reList()。
只记得一个大概了,具体规则在代码注释中提到了。
#define NULL 0
#include <stdio.h>
typedef struct LNode
{
char value;
struct LNode *next;
}LNode;
int initLi(LNode **head)//头插法
{
LNode *th=*head;
char c=getchar();
while(c!='$')
{
if(c!=' ')
{
LNode *temp=malloc(sizeof(LNode));
if(temp)
{
temp->value=c;
temp->next=th;
th=temp;
}
else
{
free(temp);
return -1;
}
}
c=getchar();
}
*head=th;
return 0;
}
void visit(LNode **head)
{
LNode *th=*head;
for(LNode *p=th;p!=NULL;p=p->next)
{
printf("%c->",p->value);

本文介绍了2016年亚信实习生笔试中的一道链表题目,要求反转链表的后半部分。作者分享了如何编写功能函数reList(),并提供了代码实现。最初的解决方案使用了额外空间,后经修改,减少了空间复杂度,通过pre和cur指针对链表指针进行修改,同时包含了空链表检查。
2224

被折叠的 条评论
为什么被折叠?



