这种方法是正常输入,然后将链表逆置,虽然pta是可以通过的(写数组应该都能通过),但明显没有下一种好
#include<stdio.h>
#include<malloc.h>
struct Node{
int data;
struct Node *next;
};
int main()
{
struct Node *node,*temp,*p,*head,*older,*news;
int count;
node=(struct Node *) malloc(sizeof(struct Node));
head=node;
int n,t;
scanf("%d",&n);
while(1)
{
scanf("%d",&t);
if(t<0) break;
count++;
temp=(struct Node *) malloc(sizeof(struct Node));
temp->data=t;
temp->next=NULL;
node->next=temp;
node=node->next;
}
if(count<n)
{
printf("NULL");return 0;
}
head=head->next;
news=NULL;
older=head;
while(older)
{
temp=older->next;
older->next=news;
news=older;
older=temp;
}
count=0;
while(1)
{
count++;
if(count==n)
{
printf("%d",news->data);return 0;
}
news=news->next;
}
}
这种解法就是头插法,很简单,我参考了下面这篇文章
https://blog.csdn.net/lovecyr/article/details/90645982
#include<stdio.h>
#include<malloc.h>
struct Node{
int data;
struct Node *next;
};
int main()
{
int k,t,i;
scanf("%d",&k);
struct Node *head,*p;
head=(struct Node *)malloc(sizeof(struct Node));
head->next=NULL;
while(scanf("%d",&t)&&t>=0)
{
p=(struct Node *)malloc(sizeof(struct Node));
p->data=t;
p->next=head->next;
head->next=p;
}
for(i=0;i<k;i++)
head=head->next;
if(head) printf("%d",head->data);
else printf("NULL");
}
记得NULL的情况
整除 ASP(VBScript) 中整除用“\”,比如 m = 5 \ 2,结果为 2。 取余 ASP(VBScr...
在刚刚过去的 Flutter Engage 活动上,我们正式发布了 Flutter 2 : 为任何平台创...
上传图片有两种方式: 1.fileReader 可以把图片解析成base64码的格式,简单粗暴 ...
phpstorm是由JetBrains公司开发的一款功能强大的PHP集成开发工具,可深刻理解用...
当下,大数据方面的就业主要有三大方向: 一是数据分析类大数据人才, 二是系统...
按照惯例,先来段废话:CKEditor是新一代的FCKeditor,是一个重新开发的版本。CK...
如果你想了解React Navigation 5.x的基本的概念,可以查看我之前的发的一篇文章...
说明 本章主要说明如何使用Magicodes.IE.Csv进行Csv导入导出. 关于Magicodes.IE ...
复制代码 代码如下: !--[if !IE]!-- 除IE外都可识别 !--![endif]-- !--[if IE] ...
前言 有位朋友去阿里面试,他说面试官给了几条查询SQL,问:需要执行几次树搜索操...