#include <stdio.h>
#include <string.h>
struct ListNode //结构体和重命名
{
int m_nKey;
struct ListNode* m_pNext;
};
typedef struct ListNode Node;
Node* creatListNode() //申请结点
{
Node* temp=(Node*)malloc(sizeof(Node));
return temp;
};
int main() {
int n=0;
int x=0,i=1;int k=0; //n 长度,x 存结点值,k 题中所给.
Node* rear=NULL,*temp=NULL,*head=NULL; //头结点,temp暂存申请节点,rear存上一结点
while( scanf("%d\n",&n)!=EOF){ //n后等待输入
i=1;rear=temp=head=NULL; //要重新赋值,多组数据会触发
while((scanf("%d",&x)!=EOF)&&i<=n){ //结点序列输入,存储
if(i<n){
scanf(" "); //空格
}
temp=creatListNode(); //申请结点并赋值
temp->m_nKey=x;
temp->m_pNext=NULL;
if(head==NULL){ //第一次就给head
head=temp;
}
if(rear==NULL){ //第一次也给尾巴
rear=temp;
}else {
rear->m_pNext=temp; //尾巴用来连接上次和刚申请的结点
rear=temp;
};
i++; //最后一次i会>n;
if(i>n) break; //直接跳出,
// printf("%d",temp->m_nKey);
}
n=0; //忘记长度
scanf("\n%d\n",&k); //输入K
//有链表head首元结点,rear尾指针,需要的倒数第K个,忘记了长度n;
i=0;
temp=head;
while(temp!=NULL){
temp=temp->m_pNext;
i++;
}
n=i; //求出长度n了
// printf("%d",k);检查k
i=0;
temp=head;
while(i<n-k){
temp=temp->m_pNext;
i++;
}
x=temp->m_nKey;
printf("%d\n",x); //找出了一组要求的。
temp=head; //我很闲,去释放了申请的结点
while(temp!=NULL){
rear=temp;
temp=temp->m_pNext;
free(rear);
rear=NULL;
}
} //下一组开始循环
return 0;
}