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;
}
相关推荐
ふり4 分钟前
图书管理系统
java·开发语言·intellij-idea
Joyner20187 分钟前
python-leetcode-相交链表
算法·leetcode·链表
qq_124987075319 分钟前
Java+SpringBoot+Vue+数据可视化的综合健身管理平台(程序+论文+讲解+安装+调试+售后)
java·开发语言·spring boot·毕业设计
煤炭里de黑猫19 分钟前
Lua C API:深入理解 lua_pushnumber 函数 — 将数字压入 Lua 栈中
开发语言·lua
哥坐11路24 分钟前
网络IP跳动问题解决详
开发语言·php
奔跑吧邓邓子1 小时前
【Python爬虫(27)】探索数据可视化的魔法世界
开发语言·爬虫·python·数据可视化
code bean1 小时前
【C# 数据结构】队列 FIFO
开发语言·数据结构·c#
Shuzi_master71 小时前
<02.21>八股文
java·开发语言
元亓亓亓1 小时前
java后端开发day18--学生管理系统
java·开发语言
EPSDA2 小时前
Linux线程池
linux·运维·服务器·开发语言·c++