题解 | #合并两个排序的链表#

合并两个排序的链表

https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

/**
 * 
 * @param pHead1 ListNode类 
 * @param pHead2 ListNode类 
 * @return ListNode类
 */
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
    // write code here
    if(pHead1==pHead2&&pHead1==NULL)return 0;
    if(pHead1==NULL&&pHead2!=NULL)return pHead2;
    if(pHead1!=NULL&&pHead2==NULL)return pHead1;
    struct ListNode* head1,*head2,*head11,*head22;
    head1=pHead1;
    head2=pHead2;
    if((pHead2->next==NULL)&&(pHead1->next==NULL)){
        if(pHead1->val>=pHead2->val){
            pHead2->next=pHead1;
            return pHead2;
        }else{
            pHead1->next=pHead2;
            return pHead1;
        }
    }
    if(pHead1->next==NULL){
        head22=head2->next;
        if(head1->val<=head2->val){
            head1->next=head2;
            return pHead1;
        }
        while(1){
            if(head1->val<=head22->val){

                head2->next=head1;
                head1->next=head22;
                return pHead2;
            }
            head2=head22;
            if(head22->next!=NULL){
                head22=head22->next;
            }
            if(head2->next==NULL){
                head2->next=head1;
                return pHead2;
            }
        }
    }
    if(pHead2->next==NULL){
        head11=head1->next;
        if(head2->val<=head1->val){
            head2->next=head1;
            return pHead2;
        }
        while(1){
            if(head2->val<=head11->val){
                head1->next=head2;
                head2->next=head11;
                return pHead1;
            }
            head1=head11;
            if(head11->next!=NULL){
                head11=head11->next;
            }
            if(head1->next==NULL){
                head1->next=head2;
                return pHead1;
            }
        }
    }
    head11=head1->next;
    head22=head2->next;
    if(head1->val<=head2->val){
        head1->next=head2;
        head22=head2;
        head2=head1;
        head1=head11;
        if(head11!=NULL){
            head11=head11->next;
        }
        while(head1!=NULL&&head22!=NULL){
            if(head1->val<=head22->val){
                head2->next=head1;
                head1->next=head22;
                head2=head1;
                head1=head11;
                if(head11!=NULL){
                    head11=head11->next;
                }

               
            }else if(head22!=NULL){
                head2=head22;
                head22=head22->next;
            }
        }
        if(head1==NULL)return pHead1;
        if(head22==NULL){
            head2->next=head1;
            return pHead1;
        }
    }
    if(head2->val<head1->val){
        head2->next=head1;
        head11=head1;
        head1=head2;
        head2=head22;
        if(head22!=NULL){
            head22=head22->next;
        }
        while(head2!=NULL&&head11!=NULL){
            if(head2->val<=head11->val){
                head1->next=head2;
                head2->next=head11;
                head1=head2;
                head2=head22;
                if(head22!=NULL){
                    head22=head22->next;
                }

            }else if(head11!=NULL){
                head1=head11;
                head11=head11->next;
            }
        }
        if(head2==NULL)return pHead2;
        if(head11==NULL){
            head1->next=head2;
            return pHead2;
        }
    }
   return pHead1;
}

全部评论

相关推荐

ProMonkey2024:5个oc?厉害! 但是有一个小问题:谁问你了?😡我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了(别的帖子偷来的,现学现卖😋)
点赞 评论 收藏
分享
10-30 10:16
南京大学 Java
龚至诚:给南大✌️跪了
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务