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;
}
相关推荐
CHTXRT2 分钟前
2025第十六届蓝桥杯大赛(软件赛)网络安全赛 Writeup
c语言·网络·web安全·网络安全·蓝桥杯·wireshark
钢铁男儿6 分钟前
C# 深入理解类:面向对象编程的核心数据结构
开发语言·数据结构·c#
Doker 多克31 分钟前
Python-Django系列—部件
开发语言·python
江沉晚呤时1 小时前
深入解析 ASP.NET Core 中的 ResourceFilter
开发语言·c#·.net·lucene
huangyuchi.1 小时前
【C++11】Lambda表达式
开发语言·c++·笔记·c++11·lambda·lambda表达式·捕捉列表
XiaoyuEr_66881 小时前
如何创建一个C#项目(基于VS2022版)
开发语言·c#
小智学长 | 嵌入式1 小时前
单片机-89C51部分:4、固件烧录
c语言·单片机·嵌入式硬件
Mercury-circle2 小时前
JavaScript基础知识合集笔记1——数据类型
开发语言·javascript·笔记
Chase_______2 小时前
Java后端开发——分层解耦详解
java·开发语言·spring·web
喝可乐的布偶猫2 小时前
Java----super 关键字
java·开发语言