题解 | #牛牛的链表交换#

牛牛的链表交换

https://www.nowcoder.com/practice/0e009fba6f3d47f0b5026b5f8b0cb1bc

#include <iostream>
#include <list>
using namespace std;


int main() {
    int a;
    cin >> a;
    int data;
    list<int> lst;
    while (cin >> data) {
        lst.push_back(data);
    }

    auto it_first = lst.begin();
    auto it_second = std::next(it_first);

    auto it_last = lst.end();
    --it_last; // Move to the last element
    auto it_second_last = std::prev(it_last);

    // Swap the first two with the last two
    std::iter_swap(it_first, it_second);
    std::iter_swap(it_last, it_second_last);

    for (auto& i : lst)
        cout << i << " ";
}
// 64 位输出请用 printf("%lld")

没什么好说的,我是来偷懒的,大家别学我

全部评论

相关推荐

牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务