每次插入的时候找到第一个大于等于输入数的位置插入就行

遍历链表

https://www.nowcoder.com/practice/7d348aa8b7d24e01a4f10bd023e2fb54

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main() {
    int n;
    cin >> n;
    list<int> l;
    while (n--) {
        int m;
        cin >> m;
        auto it = l.begin();
        while (it != l.end() && *it < m) it++;
        l.insert(it, m);
    }
    for_each(l.begin(), l.end(), [](int x) { cout << x << ' '; });
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-29 17:30
点赞 评论 收藏
分享
fRank1e:吓得我不敢去外包了,但是目前也只有外包这一个实习,我还要继续去吗
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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