当前位置: 首页 > news >正文

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;
}

相关文章:

  • 使用WPF模仿Windows记事本界面
  • 【单片机学习笔记】Windows+Vscode+STM32F4+freeRTOS+FatFs gcc环境搭建
  • 大学英语试卷
  • 数组的初始化以及拷贝
  • 使用GoogleNet网络实现花朵分类
  • 在Kubernetes(k8s)上部署整个SpringCloud微服务应用
  • Java IO流
  • 从零开始搭建第一个django项目
  • 使用序列化技术保存数据 改进 IO流完成项目实战水果库存系统
  • Kotlin中的内联函数:提升性能与解决Lambda表达式参数问题
  • 模拟器-雷电-使用adb push或adb pull操作文件
  • Android Studio初学者实例:RecyclerView学习--模仿今日头条--续
  • 向量检索库Milvus架构及数据处理流程
  • 如何系统 如何进行SQL监控-执行SQL分析打印
  • 【每日一练】20231023
  • 紫光同创FPGA实现PCIE测速试验,提供PDS工程和Linux QT上位机源码和技术支持
  • 报错:The supplied javaHome seems to be invalid. I cannot find the java executable
  • 如何开发一个 Safari 插件
  • flutter开发的一个小小小问题,内网依赖下不来
  • RemObjects Elements 12.0 Crack
  • 中国公民在日本被机动车碾压身亡,我使馆发布提醒
  • 今年一季度全国城镇新增就业308万人,就业形势保持总体稳定
  • 法治日报调查直播间“杀熟”乱象:熟客越买越贵,举证难维权不易
  • 日中友好议员联盟代表团访问中国人民对外友好协会
  • 美乌总统梵蒂冈会谈,外交部:望有关各方继续通过对话谈判解决危机
  • 俄外长与美国务卿通电话,讨论俄美关系及乌克兰问题