题解 | #牛牛的链表交换#
牛牛的链表交换
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")
没什么好说的,我是来偷懒的,大家别学我