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

遍历链表

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;
}

全部评论

相关推荐

程序员鼠鼠_春招版:都很烂大街,rpc也基本没人问,考研吧,不然就包装一段实习再去
点赞 评论 收藏
分享
02-05 08:18
四川大学 Java
在思考的熊熊很讨厌吃香菜:不是,我门头沟学院呢?这都没排上?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务