c语言练习91:合并两个有序链表

合并两个有序链表

力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

代码1:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    //判断链表是否为空
    if(list1==NULL){
        return list2;
    }
    if(list2==NULL){
        return list1;
    }
    //走到这里说明链表不为空,遍历链表
    ListNode*cur1=list1;
    ListNode*cur2=list2;
    ListNode*newhead,*newtail;
    newhead=newtail=NULL;
    while(cur1&&cur2){
        //1.空链表的情况下:插入的结点就是链表的头结点和尾结点
        //2.非空链表:插入的结点是链表的新的尾结点,头结点不变
        if(cur1->val<cur2->val){
            if(newhead==NULL){
                newhead=newtail=cur1;
            }
            else{
                newtail->next=cur1;
                newtail=newtail->next;
            }
            cur1=cur1->next;
        }
        else{//cur2<=cur1
            if(newhead==NULL){
                newhead=newtail=cur2;
            }
            else{
                newtail->next=cur2;
                newtail=newtail->next;
            }
            cur2=cur2->next;
        }
    }
    if(cur1){
        newtail->next=cur1;
    }
    if(cur2){
        newtail->next=cur2;
    }
    return newhead;
}

优化:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    //判断链表是否为空
    if(list1==NULL){
        return list2;
    }
    if(list2==NULL){
        return list1;
    }
    //走到这里说明链表不为空,遍历链表
    ListNode*cur1=list1;
    ListNode*cur2=list2;
    ListNode*newhead,*newtail;//newhead为哨兵卫
    newhead=newtail=(ListNode*)malloc(sizeof(ListNode));
    // ListNode*newhead,*newtail;
    // newhead=newtail=NULL;
    while(cur1&&cur2){
        //1.空链表的情况下:插入的结点就是链表的头结点和尾结点
        //2.非空链表:插入的结点是链表的新的尾结点,头结点不变
         if(cur1->val<cur2->val){
        //     if(newhead==NULL){
        //         newhead=newtail=cur1;
        //     }
        //     else{
        //         newtail->next=cur1;
        //         newtail=newtail->next;
        //     }
        newtail->next=cur1;
        newtail=newtail->next;
             cur1=cur1->next;
        }
        else{
            //cur2<=cur1
            // if(newhead==NULL){
            //     newhead=newtail=cur2;
            // }
            // else{
            //     newtail->next=cur2;
            //     newtail=newtail->next;
            // }
            newtail->next=cur2;
            newtail=newtail->next;
            cur2=cur2->next;
        }
    }
    if(cur1){
        newtail->next=cur1;
    }
    if(cur2){
        newtail->next=cur2;
    }
    ListNode*returnhead=newhead->next;
    free(newhead);
    return returnhead;
}

代码2:

创建一个哨兵位。

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
     if (list1 == NULL)
         return list2;
     if (list2 == NULL)
         return list1;
     struct ListNode *head =NULL, *tail = NULL;
     head = tail = (struct ListNode *)malloc(sizeof(struct ListNode));
     while (list1 && list2) {
         if (list1->val < list2->val) {
             tail->next = list1;
             tail = list1;
             list1 = list1->next;
         } else {
             tail->next = list2;
             tail = list2;
             list2 = list2->next;
         }
     }
     if (list1) {
         tail->next = list1;
     }
     if (list2) {
         tail->next = list2;
     }

     return head->next;
 }

最后返回的应是哨兵位的下一个结点。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值