建立两个非递减链表,合并成一个递增链表

建立两个非递减链表,合并成一个递增链表

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
int data;
struct node *next;
}no;


void youxu(no *head,no *p){
no *q,*tail;
q=head;
tail=q->next;

while(tail!=NULL&&tail->data<p->data){
    q=q->next;
    tail=tail->next;
}

q->next=p;
p->next=tail;


}

no *cre(){
no *head,*p;
int n;
head=new no;
head->next=NULL;
cin>>n;
while(n!=0){
 p=new no;
 p->data=n;
 p->next=NULL;

 youxu(head,p);
 cin>>n;


}

return head;

}


int main()
{
    no *head1,*head2;
    no *p,*q,*t;
    head1=cre();
    head2=cre();


     p=head1->next;
     q=head2->next;
     t=head1;

     while(p!=NULL&&q!=NULL){
     if(p->data<=q->data){
        t->next=p;
        t=p;
        p=p->next;
     } else{
         t->next=q;
         t=q;
         q=q->next;
     }
     }

     if(p==NULL){

        t->next=q;
     }else if(q==NULL){
     t->next=p;
     }

     p=head1->next->next;
     head1->next->next=NULL;

     while(p!=NULL){
        t=p->next;
        p->next=head1->next;
        head1->next=p;
        p=t;
     }


     p=head1->next;
    while(p!=NULL){
        cout<<p->data<<" ";
        p=p->next;
    }

   // cout << "Hello world!" << endl;
    return 0;
}