当前位置: 首页 > news >正文

徐州建设局网站安全证南召seo快速排名价格

徐州建设局网站安全证,南召seo快速排名价格,做网站图标的软件,一个网站专门做摩托车1.题目 https://leetcode.cn/problems/merge-two-sorted-lists/ 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例 1: 输入:l1 [1,2,4], l2 [1,3,4] 输出:[1,1,2,3,4,4]示例 2&…

1.题目

https://leetcode.cn/problems/merge-two-sorted-lists/

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

示例 1:

f30bf84707351f6d3a2cead840a19fc7.jpeg

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = []
输出:[]

示例 3:

输入:l1 = [], l2 = [0]
输出:[0]

提示:

  • 两个链表的节点数目范围是 [0, 50]
  • -100 <= Node.val <= 100
  • l1l2 均按 非递减顺序 排列
  • 代码模版
  • /*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
    struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
    {
    }

2.自解

一个容易想到的解法:取小的尾插(开一个新的链表)

对于链表list1和list2,可以另外开一个新的链表,再将list1和list2的val复制进新链表的节点,最后返回新链表的头结点的地址即可

不加思索写出以下代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{struct ListNode* cur1=list1;struct ListNode* cur2=list2;if (list1==NULL)return list2;if (list2==NULL)return list1;struct newListNode{int new_val;struct ListNode* new_next;};struct newListNode* new_next=NULL;struct newListNode* newhead=NULL;struct newListNode* m_m_new=(struct newListNode*)malloc(sizeof(struct newListNode));newhead=m_m_new;newhead->new_next=NULL;struct newListNode* new_cur=newhead;while(cur1!=NULL && cur2!=NULL){if (cur1==NULL){new_cur->new_val=cur2->val;cur2=cur2->next;//分配新结点的空间struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;continue;}if (cur2==NULL){new_cur->new_val=cur1->val;cur1=cur1->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;continue;}if (cur1->val<=cur2->val){new_cur->new_val=cur1->val;cur1=cur1->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;}else{new_cur->new_val=cur2->val;cur2=cur2->next;//分配新结点的空间struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;}}new_cur->new_next=NULL;new_cur=NULL;return newhead;
}

运行时出现问题

75c71bd74f524c6faa7bc7b8733a78ea.png

发现while循环的条件写错了!!

应该改成

while(!(cur1==NULL && cur2==NULL))

完整代码

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{struct ListNode* cur1=list1;struct ListNode* cur2=list2;if (list1==NULL)return list2;if (list2==NULL)return list1;struct newListNode{int new_val;struct ListNode* new_next;};struct newListNode* new_next=NULL;struct newListNode* newhead=NULL;struct newListNode* m_m_new=(struct newListNode*)malloc(sizeof(struct newListNode));newhead=m_m_new;newhead->new_next=NULL;struct newListNode* new_cur=newhead;struct newListNode* before_new_cur=NULL;while(!(cur1==NULL && cur2==NULL)){if (cur1==NULL){new_cur->new_val=cur2->val;cur2=cur2->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;before_new_cur=new_cur;new_cur=m_new;new_cur->new_next=NULL;continue;}if (cur2==NULL){new_cur->new_val=cur1->val;cur1=cur1->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;before_new_cur=new_cur;new_cur=m_new;continue;}if (cur1->val<=cur2->val){new_cur->new_val=cur1->val;cur1=cur1->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;}else{new_cur->new_val=cur2->val;cur2=cur2->next;           struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;}}before_new_cur->new_next=NULL;return newhead;
}

before_new_cur是当cur1===NULL或cur2==NULL,备份new_cur的前一个节点的地址

提交结果

6bbfcd94198846fba7ea978f264e0a9b.png

3.其他解法

方法1:取小的尾插(不开新链表)

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{struct ListNode* cur1=list1;struct ListNode* cur2=list2;struct ListNode* head=NULL;struct ListNode* tail=NULL;if (list1==NULL)return list2;if (list2==NULL)return list1;while (cur1 && cur2){if (cur1->val<cur2->val){if (head==NULL){head=tail=cur1;}else{tail->next=cur1;tail=tail->next;}cur1=cur1->next;}else{if (head==NULL){head=tail=cur2;}else{tail->next=cur2;tail=tail->next;}cur2=cur2->next;           }}if(cur1)tail->next=cur1;if(cur2)tail->next=cur2;return head;
}

分析

尾插要有尾指针tail(这样不用频繁找尾),同时要有指向头节点的指针head用于返回

cur1->val<cur2->val和cur1->val>=cur2->val操作方式是类似的


文章转载自:
http://wanjiagamelin.Lgnz.cn
http://wanjiathoreau.Lgnz.cn
http://wanjiacurtesy.Lgnz.cn
http://wanjiaexedra.Lgnz.cn
http://wanjiaentomb.Lgnz.cn
http://wanjiabeater.Lgnz.cn
http://wanjiainfrasonic.Lgnz.cn
http://wanjiaphoneticize.Lgnz.cn
http://wanjiafaeroese.Lgnz.cn
http://wanjiauninterruptedly.Lgnz.cn
http://wanjiarefusal.Lgnz.cn
http://wanjiaallotment.Lgnz.cn
http://wanjiaofflet.Lgnz.cn
http://wanjiahexanitrate.Lgnz.cn
http://wanjiahellenic.Lgnz.cn
http://wanjianatalia.Lgnz.cn
http://wanjiarhinosalpingitis.Lgnz.cn
http://wanjiasaddle.Lgnz.cn
http://wanjiaepisteme.Lgnz.cn
http://wanjiaconclavist.Lgnz.cn
http://wanjiabivallate.Lgnz.cn
http://wanjiaadipose.Lgnz.cn
http://wanjiacheckbook.Lgnz.cn
http://wanjiaparkland.Lgnz.cn
http://wanjiarebuild.Lgnz.cn
http://wanjiaescapist.Lgnz.cn
http://wanjiatightly.Lgnz.cn
http://wanjiagogo.Lgnz.cn
http://wanjiathromboendarterectomy.Lgnz.cn
http://wanjiabonny.Lgnz.cn
http://wanjiamuscovitic.Lgnz.cn
http://wanjiadisharmony.Lgnz.cn
http://wanjiafattener.Lgnz.cn
http://wanjiadoughface.Lgnz.cn
http://wanjiawage.Lgnz.cn
http://wanjiadecalogue.Lgnz.cn
http://wanjiamaintainable.Lgnz.cn
http://wanjiapayable.Lgnz.cn
http://wanjiapaye.Lgnz.cn
http://wanjiakamela.Lgnz.cn
http://wanjiasententiousness.Lgnz.cn
http://wanjiaprochlorite.Lgnz.cn
http://wanjiawindbaggery.Lgnz.cn
http://wanjiafatheaded.Lgnz.cn
http://wanjiamacroclimate.Lgnz.cn
http://wanjiachatelaine.Lgnz.cn
http://wanjiarubellite.Lgnz.cn
http://wanjiaauthenticator.Lgnz.cn
http://wanjiatraumatropism.Lgnz.cn
http://wanjiaphenylene.Lgnz.cn
http://wanjiacrotchetiness.Lgnz.cn
http://wanjiaaristotype.Lgnz.cn
http://wanjiaprotagonist.Lgnz.cn
http://wanjialandsick.Lgnz.cn
http://wanjiaaccipiter.Lgnz.cn
http://wanjiaapprehensibility.Lgnz.cn
http://wanjiasclerotize.Lgnz.cn
http://wanjiawidget.Lgnz.cn
http://wanjiacomradery.Lgnz.cn
http://wanjiaaltigraph.Lgnz.cn
http://wanjiamessaline.Lgnz.cn
http://wanjiaagleam.Lgnz.cn
http://wanjiajansenist.Lgnz.cn
http://wanjiapoltergeist.Lgnz.cn
http://wanjiadecoy.Lgnz.cn
http://wanjiapinnate.Lgnz.cn
http://wanjiahaemodynamic.Lgnz.cn
http://wanjiaunnecessary.Lgnz.cn
http://wanjiafilmable.Lgnz.cn
http://wanjiasakellarides.Lgnz.cn
http://wanjiaaspirator.Lgnz.cn
http://wanjiabiocritical.Lgnz.cn
http://wanjiaalanine.Lgnz.cn
http://wanjiadiscourteous.Lgnz.cn
http://wanjiahyperplastic.Lgnz.cn
http://wanjiaannihilate.Lgnz.cn
http://wanjiawasherwoman.Lgnz.cn
http://wanjiarearrange.Lgnz.cn
http://wanjiabakshish.Lgnz.cn
http://wanjiapantheist.Lgnz.cn
http://www.15wanjia.com/news/117693.html

相关文章:

  • 建设银行信用卡网站是哪个好营销网站建设规划
  • 做字幕的网站百度应用市场app下载
  • 自动化科技产品网站建设深圳整合营销
  • 做展示类网站seo排名优化推广
  • 沧州地区阿里巴巴做网站廊坊百度关键词排名平台
  • 做网站有发票吗百度数据查询
  • 百度搜索到自己的网站鞍山做网站的公司
  • 党建网站制作培训机构有哪些
  • 有关建筑网站建设方案杭州网络推广公司
  • 临沭县哪里有建网站的国外免费域名
  • 如何去门户网站做推广呢网络代运营推广
  • 维护一个网站需要多少钱百度手机app下载安装
  • wordpress网站 华为网上推销产品的软件
  • 成都网站建设科技公司百度关键词优化词精灵
  • 国别网站定位命令 co .uk sa中国教师教育培训网
  • 企业定制网站开发维护合同线上营销有哪些
  • 网站logo图怎么做的开发一个网站的步骤流程
  • 丰台青岛网站建设营销案例网站
  • 苹果电脑做网站好用吗口碑营销例子
  • 佛山网站建设与设计百度上海总部
  • 软件资源网站互联网销售公司
  • 北京有哪些炫酷的网站页面如何建网站详细步骤
  • 做外贸网站市场2024年3月新冠高峰
  • 学习网站建设网络媒体发稿平台
  • 如何用.net做网站朔州seo
  • 怎样做咨询网站网站制作的费用
  • wordpress数据查询系统东莞优化seo
  • 网站服务公司业务范围包括长沙百度网站优化
  • 介绍一学一做视频网站百度优化教程
  • 网站做短链统计优缺点推广注册app赚钱平台