两个升序链表,合并成一个升序链表

Node* mergeAction(Node* head1, Node* head2){
//两个链表的合并操作
        Node* head=(Node*)malloc(sizeof(Node));
        Node* q=head;
        while(head1 && head2){
                if(head1->data<=head2->data){
                        Node* p=(Node*)malloc(sizeof(Node));
                        p->data=head1->data;
                        p->next=NULL;
                        q->next=p;
                        q=q->next;
                        head1=head1->next;
                }
                else if(head1->data > head2->data){
                        Node* p=(Node*)malloc(sizeof(Node));
                        p->data=head2->data;
                        p->next=NULL;
                        q->next=p;
                        q=q->next;
                        head2=head2->next;
                }
        }
        return head->next;
}

你可能感兴趣的:(职场,休闲,链表合并)