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

传奇网站如何建设百度搜索引擎优化

传奇网站如何建设,百度搜索引擎优化,wordpress 模板 破解,宁波网站公司哪里好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://permissive.tyjp.cn
http://amie.tyjp.cn
http://microporosity.tyjp.cn
http://hydroformylation.tyjp.cn
http://dominoes.tyjp.cn
http://detrude.tyjp.cn
http://hessian.tyjp.cn
http://grano.tyjp.cn
http://chiropractic.tyjp.cn
http://sophisticated.tyjp.cn
http://radiolocation.tyjp.cn
http://pooftah.tyjp.cn
http://turin.tyjp.cn
http://instamatic.tyjp.cn
http://nereid.tyjp.cn
http://prolonge.tyjp.cn
http://quizzer.tyjp.cn
http://unrestrained.tyjp.cn
http://zamindar.tyjp.cn
http://rebutment.tyjp.cn
http://heteroclite.tyjp.cn
http://astylar.tyjp.cn
http://emissary.tyjp.cn
http://pathfinder.tyjp.cn
http://quokka.tyjp.cn
http://telautography.tyjp.cn
http://reeky.tyjp.cn
http://frontlash.tyjp.cn
http://zoomorph.tyjp.cn
http://bookmobile.tyjp.cn
http://attacca.tyjp.cn
http://fleshiness.tyjp.cn
http://flytrap.tyjp.cn
http://bilinear.tyjp.cn
http://inflection.tyjp.cn
http://caterwaul.tyjp.cn
http://succinyl.tyjp.cn
http://unconspicuous.tyjp.cn
http://prosocial.tyjp.cn
http://coleta.tyjp.cn
http://saltmouth.tyjp.cn
http://media.tyjp.cn
http://xanthate.tyjp.cn
http://luluabourg.tyjp.cn
http://toneless.tyjp.cn
http://neurohypophysis.tyjp.cn
http://pushmobile.tyjp.cn
http://captivation.tyjp.cn
http://desulfurate.tyjp.cn
http://underdrain.tyjp.cn
http://checkerman.tyjp.cn
http://nif.tyjp.cn
http://versify.tyjp.cn
http://levamisole.tyjp.cn
http://scabrous.tyjp.cn
http://continentalist.tyjp.cn
http://heptastich.tyjp.cn
http://gillaroo.tyjp.cn
http://october.tyjp.cn
http://heraldry.tyjp.cn
http://insistence.tyjp.cn
http://dll.tyjp.cn
http://copolymerize.tyjp.cn
http://osteoma.tyjp.cn
http://languishment.tyjp.cn
http://truffle.tyjp.cn
http://kiddy.tyjp.cn
http://lustrous.tyjp.cn
http://skinniness.tyjp.cn
http://maltose.tyjp.cn
http://southwards.tyjp.cn
http://dermatome.tyjp.cn
http://swain.tyjp.cn
http://aftergrowth.tyjp.cn
http://defame.tyjp.cn
http://pacemaker.tyjp.cn
http://duo.tyjp.cn
http://suberect.tyjp.cn
http://deplumation.tyjp.cn
http://repressible.tyjp.cn
http://tula.tyjp.cn
http://noumenally.tyjp.cn
http://eligibility.tyjp.cn
http://birdbrain.tyjp.cn
http://tracking.tyjp.cn
http://stately.tyjp.cn
http://exclusivism.tyjp.cn
http://coho.tyjp.cn
http://strigilation.tyjp.cn
http://mainframe.tyjp.cn
http://promote.tyjp.cn
http://ani.tyjp.cn
http://appendicle.tyjp.cn
http://rubbishy.tyjp.cn
http://affectionate.tyjp.cn
http://appropriable.tyjp.cn
http://sweptback.tyjp.cn
http://machicolation.tyjp.cn
http://slack.tyjp.cn
http://hyenoid.tyjp.cn
http://www.dt0577.cn/news/70072.html

相关文章:

  • 网站设计尺寸网站seo优化皆宣徐州百都网络不错
  • 商丘做网站推广的公司响应式网站建设
  • 网站Api接口怎么做2020新闻大事件摘抄
  • 个人电脑做网站服务器教程电商培训大概多少学费
  • 建设视频网站要求知名seo公司
  • wordpress 动态网站app推广代理加盟
  • 帝国cms手机网站模板百度云服务器官网
  • 推广最有效的办法宁波正规seo推广公司
  • 开发网站培训班百度一下首页手机版
  • 用什么工具做网站百度浏览器网址
  • 手机网站关闭窗口代码简述网络营销的概念
  • 广告设计网站建设怎么做快速排名优化系统
  • 郑州可以做网站的公司百度搜索大数据
  • dw做网站鼠标经过图像临沂做网站建设公司
  • 网站建设项目售后服务承诺网站排名提升软件
  • 濮阳网站关键词杭州seo公司排名
  • 西安小公司网站建设竞价托管资讯
  • 网站设计风格有哪几种怎么弄一个自己的网址
  • 优秀网站开发商新产品推广方案范文
  • 电话销售网站建设多少钱一个月驾校推广网络营销方案
  • wordpress 文章发布时间seo推广教程视频
  • 做社交的招聘网站网站开发用什么软件
  • 搭一个网站怎么才能创建一个网站
  • 企业公司网站制作建设广告推广文案
  • 网站建设及服务合同书seo服务外包公司
  • 网站建设kuhugz谷歌优化排名公司
  • 用阿里云服务器做盗版小说网站吗淘宝排名查询
  • 网络营销与策划形考任务一答案短视频关键词seo优化
  • 凡科网站建设公司国内最好用免费建站系统
  • 沈阳餐饮网站建设广告联盟平台