PAT2-11. 两个有序链表序列的合并

#include<iterator>
#include<list>
#include<cstdio>
using namespace std;
int main(){
  list<int>a,b,c;
  int k;
  while(scanf("%d",&k),k!=-1)    a.push_back(k);
  while(scanf("%d",&k),k!=-1)    b.push_back(k);

  auto it1=a.begin(),it2=b.begin();
  auto it3=back_inserter(c);
  
  while(it1!=a.end() && it2!=b.end())
    *it3++=(*it1<*it2?*it1++:*it2++);
  if(it1==a.end())    copy(it2,b.end(),it3);
  if(it2==b.end())    copy(it1,a.end(),it3);
  for(auto x:c){
    static bool first=true;
    if(first)first=false;
    else putchar(' ');
    printf("%d",x);
  }
  if(c.size()==0)printf("NULL");
  return 0;
}
两个有序序列的merge

你可能感兴趣的:(PAT2-11. 两个有序链表序列的合并)