题解 | #遍历链表#用链表的方式做,边插入边排序
遍历链表
https://www.nowcoder.com/practice/7d348aa8b7d24e01a4f10bd023e2fb54
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Lnode
{
int val;
struct Lnode*next;
Lnode(int x):val(x),next(nullptr){};
};
int main() {
int n;
while(cin>>n)
{
int val;
Lnode* L=new Lnode(-9999);
Lnode* pre=L;
for(int i=0;i<n;i++)
{
cin>>val;
Lnode* temp=new Lnode(val);
if(val>pre->val)
{
pre->next=temp;
pre=pre->next;
}
else
{
Lnode* p=L;
while(p->next->val<val)
{
p=p->next;
}
temp->next=p->next;
p->next=temp;
}
}
while(L->next)
{
L=L->next;
cout<<L->val<<" ";
}
cout<<endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
查看22道真题和解析