描述 | |
---|---|
知识点 | 链表,查找,指针 |
运行时间限制 | 10M |
内存限制 | 128 |
输入 | 输入说明 |
输出 | 输出一个整数 |
样例输入 | 8 1 2 3 4 5 6 7 8 4 |
样例输出 | 4 |
#include<iostream>
#include<cstdlib>
using namespace std;
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
int main()
{
int num,key;
cin>>num;
struct ListNode *head=NULL;
struct ListNode *pos=NULL;
struct ListNode *place1,*place2;
for(int i=0;i<num;i++)
{
struct ListNode *temp=(struct ListNode *)malloc(sizeof(struct ListNode));
cin>>temp->m_nKey;
temp->m_pNext=NULL;
if(pos==NULL)pos=temp;
else
{
pos->m_pNext=temp;
pos=pos->m_pNext;
}
if(head==NULL)head=pos;
}
cin>>key;
place1=place2=head;
while (key-->=0)
{
place2=place2->m_pNext;
}
while(place2!=NULL)
{
place1=place1->m_pNext;
place2=place2->m_pNext;
}
cout<<place1->m_nKey<<endl;
return 0;
}