当前位置:首页 » 《资源分享》 » 正文

【力扣LeetCode】合并两个有序链表_Layman光~的博客

26 人参与  2021年10月31日 11:43  分类 : 《资源分享》  评论

点击全文阅读


题目:

将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
在这里插入图片描述

题目分析:

在这里插入图片描述
解决这个问题的方法是,可以将两个链表中的数据依次比较,然后我们可以将较小的取下来尾插,就这样依次进行,直到其中一个走到空,就可以结束了。

代码实现:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
    if(l1 == NULL)
        return l2;
    if(l2 == NULL)
        return l1;
    struct ListNode*n1,*n2;
    n1 = l1;
    n2 = l2;
    struct ListNode*newhead,*tail;
    newhead = tail = NULL;
    while(n1 && n2)
    {
        if(n1->val<n2->val)
        {
            if(tail == NULL)
            {
                newhead = tail = n1;
            }
            else
            {
                tail->next = n1;
                tail = n1;
            }
            n1 = n1->next;
        }
        else
        {
            if(tail == NULL)
            {
                newhead = tail = n2;
            }
            else
            {
                tail->next = n2;
                tail = n2;
            }
            n2 = n2->next;
        }
    }
    if(n1)
    {
        tail->next = n1;
    }
    if(n2)
    {
        tail->next = n2;
    }
    return newhead;

}

优化方案:

看上面的代码,我们可能会发现其中的循环有点冗余,因此我们可以改进一下,我们可以在这两个链表中首先比较找出一个较小的值作为头节点,然后进行尾插;我们还可以用一个带哨兵位的头节点来解决这个问题。下面我们就分别给出两端优化的代码:

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
    if(l1 == NULL)
        return l2;
    if(l2 == NULL)
        return l1;
    struct ListNode*n1,*n2;
    n1 = l1;
    n2 = l2;
    struct ListNode*newhead,*tail;
    newhead = tail = NULL;
    //先取一个小的做头节点
    if(n1->val < n2->val)
    {
        newhead = tail = n1;
        n1 = n1->next;
    }
    else
    {
        newhead = tail = n2;
        n2 = n2->next;
    }
    while(n1 && n2)
    {
        if(n1->val<n2->val)
        {
            tail->next = n1;
            tail = n1;
            n1 = n1->next;
        }
        else
        {
            tail->next = n2;
            tail = n2;
            n2 = n2->next;
        }
    }
    if(n1)
    {
        tail->next = n1;
    }
    if(n2)
    {
        tail->next = n2;
    }
    return newhead;

}
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
    if(l1 == NULL)
        return l2;
    if(l2 == NULL)
        return l1;
    struct ListNode*n1,*n2;
    n1 = l1;
    n2 = l2;
    struct ListNode*newhead,*tail;
    newhead = tail = NULL;
    //给一个哨兵位的头节点,方便尾插,处理完以后,再删掉。
    newhead = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
    while(n1 && n2)
    {
        if(n1->val<n2->val)
        {
            tail->next = n1;
            tail = n1;
            n1 = n1->next;
        }
        else
        {
            tail->next = n2;
            tail = n2;
            n2 = n2->next;
        }
    }
    if(n1)
    {
        tail->next = n1;
    }
    if(n2)
    {
        tail->next = n2;
    }
    struct ListNode* first = newhead->next;
    free(newhead);
    return first;

}

点击全文阅读


本文链接:http://m.zhangshiyu.com/post/30305.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 山海不相逢内容精选(温逸尘沈衿)_山海不相逢内容精选(温逸尘沈衿)
  • (番外)+(全书)霍沉洲沈青禾此去经年人未还(霍沉洲沈青禾)_(霍沉洲沈青禾此去经年人未还)列表_笔趣阁(霍沉洲沈青禾)
  • (番外)+(全书)霍沉洲沈青禾(此去经年人未还霍沉洲沈青禾)完结_(霍沉洲沈青禾)列表_笔趣阁(此去经年人未还霍沉洲沈青禾)
  • 「重回八零,拒绝替嫁冲喜」章节彩蛋限时释出‌_卫东玉兰苏夏人气小说未删减节选
  • 重生七零祁同伟不再是农民儿子结局+番外纯净版全书免费重生七零祁同伟不再是农民儿子结局+番外纯净版全书免费
  • 傅雅宁的神女老婆,却在背地承欢作乐顾尘傅雅宁全书在线
  • 全文神女老婆,却在背地承欢作乐全局(顾尘傅雅宁)列表_全文神女老婆,却在背地承欢作乐全局
  • (番外)+(全书)此去经年人未还全书+番外+后续免费下载_(沈青禾霍沉洲)此去经年人未还全书+番外+后续列表_笔趣阁(沈青禾霍沉洲)
  • 完结文毁容的姐姐和瞎眼的我离开后,姜家两兄弟悔哭了+后续列表_完结文毁容的姐姐和瞎眼的我离开后,姜家两兄弟悔哭了+后续(林梦婉)
  • 妻子辱我爸受贿自杀,我掏出一等军功章节选推荐_[陈素云辰朋友]小说精彩章节分享
  • 全书浏览苔藓爬满旧日诺言新上(顾砚廷慕晚夏)_苔藓爬满旧日诺言新上(顾砚廷慕晚夏)全书结局
  • 顾尘傅雅宁(神女老婆,却在背地承欢作乐+后续+结局)结局_(顾尘傅雅宁神女老婆,却在背地承欢作乐+后续+结局全书结局)结局列表_笔趣阁(顾尘傅雅宁)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1