题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
int n;
while(cin >> n){
forward_list<int> list;
int head, del;
cin >> head;
list.push_front(head);
auto front = list.begin();
while(n-- > 1){
int val;
cin >> val;
list.insert_after(front, val);
front++;
}
cin >> del;
auto left = list.begin();
auto right = left;
for(int i = 0; i < del; i++){
right++;
}
while(right != list.end()){
left++;
right++;
}
cout << *left << endl;
}
return 0;
}
