有三个非递减的链表,要求将重复的元素重新置于a链表中,并且其复杂度为O(n+M+P)
- #include <iostream>
- #include <list>
- using namespace std;
- void main()
- {
- int a[10]={1,7,8,8,9,9,11,14,15,19};
- int b[8]={0,1,5,7,11,15,15,19};
- int c[9]={1,1,6,7,7,9,11,15,19};
- list<int> a_list(a,a+10),b_list(b,b+8),c_list(c,c+9);//链表赋值的方法
- list<int>::iterator a_it=a_list.begin(),b_it=b_list.begin(),c_it=c_list.begin();//迭代器初始化
- while(a_it!=a_list.end() && b_it!=b_list.end() && c_it!=c_list.end())
- {
- list<int>::iterator temp_it;
- int temp=*a_it;
- if(temp>*b_it)
- {
- b_it++;
- }
- if(temp>*c_it)
- {
- c_it++;
- }
- if(temp<*b_it || temp<*c_it)
- {
- temp_it=a_it;
- a_it++;
- a_list.erase(temp_it,a_it);
- }
- if(temp==*b_it&&temp==*c_it)
- {
- int temp_1=*(--b_it);
- int temp_2=*(--c_it);
- if(temp!=temp_1||temp!=temp_2)//如果重复则删除元素
- {
- a_it++;
- b_it++;
- b_it++;
- c_it++;
- c_it++;
- }
- else
- {
- b_it++;
- b_it++;
- c_it++;
- c_it++;
- }
- }
- }
- if(a_it!=a_list.end())//最后还没有到a_list的结尾,则把最后的元素全部去掉
- {
- a_list.erase(a_it,a_list.end());
- }
- for(a_it=a_list.begin();a_it!=a_list.end();a_it++)//输出元素
- {
- cout<<*a_it<<endl;
- }
- }