题解 | #遍历链表#

遍历链表

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

#include <iostream>
using namespace std;
int n;
struct node{
    int v;
    struct node* next;
};
typedef struct node* list;
int main() {
    while(cin >> n)
    {
        list head = (list)malloc(sizeof(list));
        head->v = 0;
        head->next = NULL;        
        list tail = head;
        for (int i = 1; i <= n; i++)
        {
            list tmp = (list)malloc(sizeof(list));
            cin >> tmp->v;
            if(tmp->v > tail->v)
            {
                tmp->next = NULL;
                tail->next = tmp;
                tail = tail->next;
            }
            else 
            {
                list l = head;
                while(l->next->v <= tmp->v)
                {
                    l = l->next;
                }
                tmp->next = l->next;
                l->next = tmp;
            }
        }
        while(head->next)
        {
            cout << head->next->v << " ";
            head = head->next;
        }
        cout << endl;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 12:05
点赞 评论 收藏
分享
Lorn的意义:你这种岗位在中国现在要么牛马天天加班,要么关系户进去好吃好喝,8年时间,真的天翻地覆了,对于资本来说你就说一头体力更好的牛马,哎,退伍没有包分配你真的亏了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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