Link: 4273. 链表合并 - AcWing题库
转置短链表采用 进栈的方法实现 。
输出两个长链表 在输出一个短链表
循环控制如下
代码没按思路走
#include#include #include #include #include using namespace std; typedef struct node{ int a,next; }node; const int maxn=1e5+10; node a[maxn]; node l1[maxn]; node l2[maxn]; int l1n,l2n; stack< pair > st; void fun(int x,int y){ for(int i=x;i!=-1;i=a[i].next){ st.push({i,a[i].a}); } int i=y; for(;i!=-1;){ if(a[i].next!=-1) printf("%05d %d %05dn",i,a[i].a,a[i].next); else printf("%05d %d -1n",i,a[i].a); i=a[i].next;if(i==-1) return ; if(st.size()){ auto it=st.top();st.pop(); printf("%05d %d %05dn",i,a[i].a,it.first); i=a[i].next; if(i!=-1) printf("%05d %d %05dn",it.first,it.second,i); else printf("%05d %d -1n",it.first,it.second); }else{ if(a[i].next!=-1) printf("%05d %d %05dn",i,a[i].a,a[i].next); else printf("%05d %d -1n",i,a[i].a); i=a[i].next; } } int main(){ l1n=l2n=0; int h1,h2,n; cin>>h1>>h2>>n; int ad; for(int i=0;i cin>>ad; cin>>a[ad].a>>a[ad].next; } for(int i=h1;i!=-1;i=a[i].next){ l1n++; } for(int i=h2;i!=-1;i=a[i].next){ l2n++; } if(l1n fun(h1,h2); }else{ fun(h2,h1); } }



