题解 | #输出单向链表中倒数第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;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务