• 代码nullMerge two sorted linked lists


    在写这篇文章之前,xxx已经写过了几篇关于改代码null主题的文章,想要了解的朋友可以去翻一下之前的文章

        标题:

        Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

        代码如下:

           struct ListNode {
                int val;
                ListNode *next;
                ListNode(int x) : val(x), next(NULL) {}
           };

        每日一道理
    如果说生命是一座庄严的城堡,如果说生命是一株苍茂的大树,如果说生命是一只飞翔的海鸟。那么,信念就是那穹顶的梁柱,就是那深扎的树根,就是那扇动的翅膀。没有信念,生命的动力便荡然无存;没有信念,生命的美丽便杳然西去。(划线处可以换其他词语)

            ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
            if(l1==NULL)return l2;
            if(l2==NULL)return l1;
            ListNode *head1=NULL,*head2=NULL;
            if(l1->val<=l2->val)
            {
                head1=l1;
                head2=l2;
            }
            else
            {
                head1=l2;
                head2=l1;
            }
            ListNode *head=head1;
            while(head2!=NULL)
            {
                while(head1->next!=NULL&&head1->next->val<=head2->val)
                {
                   head1=head1->next;
                }
                if(head1->next==NULL)
                {
                    head1->next=head2;
                    break;
                }
                else
                {
                   ListNode *tmp=head2;
                   head2=head2->next;
                   tmp->next=head1->next;
                   head1->next=tmp;
                   head1=tmp;
                }
            }
            return head;
        }

    文章结束给大家分享下程序员的一些笑话语录: 人在天涯钻,哪儿能不挨砖?日啖板砖三百颗,不辞长做天涯人~

  • 相关阅读:
    Tarjan算法与割点割边
    kmp匹配
    最小表示法
    字典树
    哈希
    网络流入门浅谈
    关于两道搜索的题目
    2020 4.2校内测题解
    LIS最长上升子序列讲解&&洛谷P1439 【模板】最长公共子序列 题解
    浅谈拉格朗日插值公式
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3087215.html
Copyright © 2020-2023  润新知