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

合并两个有序链表

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

代码1:

cpp 复制代码
/**
 * 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;
}

优化:

cpp 复制代码
/**
 * 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;
}
相关推荐
SteveDraw28 分钟前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
十五年专注C++开发38 分钟前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
flyair_China1 小时前
【云架构】
开发语言·php
Chef_Chen1 小时前
从0开始学习R语言--Day20-ARIMA与格兰杰因果检验
开发语言·学习·r语言
zh_xuan1 小时前
c++ std::pair
开发语言·c++
CodeWithMe2 小时前
【C/C++】EBO空基类优化介绍
开发语言·c++
404.Not Found2 小时前
Day46 Python打卡训练营
开发语言·python
love530love2 小时前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
凌辰揽月2 小时前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
海奥华22 小时前
go中的接口返回设计思想
开发语言·后端·golang