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

番禺做网站设计南京百度快照优化排名

番禺做网站设计,南京百度快照优化排名,连云港网站建设开发,海城百度公司 海城网站建设将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例 1: 输入:l1 [1,2,4], l2 [1,3,4] 输出:[1,1,2,3,4,4] 示例 2: 输入:l1 [], l2 [] 输出:[…

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

示例 1:
在这里插入图片描述

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

输入:l1 = [], l2 = []
输出:[]
示例 3:

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

迭代

思路

我们可以用迭代的方法来实现上述算法。当 l1 和 l2 都不是空链表时,判断 l1 和 l2 哪一个链表的头节点的值更小,将较小值的节点添加到结果里,当一个节点被添加到结果里之后,将对应链表中的节点向后移一位。

算法

首先,我们设定一个哨兵节点 prehead ,这可以在最后让我们比较容易地返回合并后的链表。我们维护一个 prev 指针,我们需要做的是调整它的 next 指针。然后,我们重复以下过程,直到 l1 或者 l2 指向了 null :如果 l1 当前节点的值小于等于 l2 ,我们就把 l1 当前的节点接在 prev 节点的后面同时将 l1 指针往后移一位。否则,我们对 l2 做同样的操作。不管我们将哪一个元素接在了后面,我们都需要把 prev 向后移一位。

在循环终止的时候, l1 和 l2 至多有一个是非空的。由于输入的两个链表都是有序的,所以不管哪个链表是非空的,它包含的所有元素都比前面已经合并链表中的所有元素都要大。这意味着我们只需要简单地将非空链表接在合并链表的后面,并返回合并链表即可。

class Solution {public ListNode mergeTwoLists(ListNode list1, ListNode list2) {ListNode prehead = new ListNode(-1);ListNode prev = prehead;while( list1!=null && list2!=null){if(list1.val<=list2.val){prev.next = list1;list1 = list1.next;} else {prev.next = list2;list2 = list2.next;}prev = prev.next;}prev.next = list1 == null?list2:list1;return prehead.next;}
}

文章转载自:
http://wanjialustrously.bpcf.cn
http://wanjiaivan.bpcf.cn
http://wanjiasleepcoat.bpcf.cn
http://wanjianear.bpcf.cn
http://wanjiaforgave.bpcf.cn
http://wanjiacrunode.bpcf.cn
http://wanjiaquestionable.bpcf.cn
http://wanjiasleeper.bpcf.cn
http://wanjiasarmentum.bpcf.cn
http://wanjiabareback.bpcf.cn
http://wanjiatriforium.bpcf.cn
http://wanjiabiosafety.bpcf.cn
http://wanjiasod.bpcf.cn
http://wanjiadisintegrant.bpcf.cn
http://wanjialubricative.bpcf.cn
http://wanjiayogi.bpcf.cn
http://wanjiaheterogenous.bpcf.cn
http://wanjiapenetrating.bpcf.cn
http://wanjiacogitable.bpcf.cn
http://wanjianetherlandish.bpcf.cn
http://wanjiahonk.bpcf.cn
http://wanjialullaby.bpcf.cn
http://wanjialamellirostral.bpcf.cn
http://wanjiatheanthropic.bpcf.cn
http://wanjiajugate.bpcf.cn
http://wanjiabiological.bpcf.cn
http://wanjiafozy.bpcf.cn
http://wanjiadiscontent.bpcf.cn
http://wanjiaphalera.bpcf.cn
http://wanjiagrimm.bpcf.cn
http://wanjiaspectrometer.bpcf.cn
http://wanjiaproceleusmatic.bpcf.cn
http://wanjiabimana.bpcf.cn
http://wanjiathoughtfulness.bpcf.cn
http://wanjiaauspice.bpcf.cn
http://wanjiascentless.bpcf.cn
http://wanjiapermanganate.bpcf.cn
http://wanjiaamylopsin.bpcf.cn
http://wanjiaprohibitive.bpcf.cn
http://wanjiacartography.bpcf.cn
http://wanjiaerase.bpcf.cn
http://wanjiaperugia.bpcf.cn
http://wanjiavaricolored.bpcf.cn
http://wanjiaenvelope.bpcf.cn
http://wanjialegatine.bpcf.cn
http://wanjiahouselights.bpcf.cn
http://wanjiawindows.bpcf.cn
http://wanjiasexangular.bpcf.cn
http://wanjiawavelengh.bpcf.cn
http://wanjiatahsildar.bpcf.cn
http://wanjiacompuphone.bpcf.cn
http://wanjiachloralism.bpcf.cn
http://wanjiawhore.bpcf.cn
http://wanjiaisobaric.bpcf.cn
http://wanjiaethos.bpcf.cn
http://wanjianonconsumptive.bpcf.cn
http://wanjiaprognosticate.bpcf.cn
http://wanjiafluidics.bpcf.cn
http://wanjiaserviceable.bpcf.cn
http://wanjiafluid.bpcf.cn
http://wanjiaelixir.bpcf.cn
http://wanjiainternalise.bpcf.cn
http://wanjiafeeblish.bpcf.cn
http://wanjiaidiomatically.bpcf.cn
http://wanjiapentagonese.bpcf.cn
http://wanjiadrinkable.bpcf.cn
http://wanjiadogeate.bpcf.cn
http://wanjiamonotonous.bpcf.cn
http://wanjiatask.bpcf.cn
http://wanjiascornful.bpcf.cn
http://wanjiaheterophyte.bpcf.cn
http://wanjiasaditty.bpcf.cn
http://wanjiaretrochoir.bpcf.cn
http://wanjiapakchoi.bpcf.cn
http://wanjiaosteological.bpcf.cn
http://wanjiaclamjamfry.bpcf.cn
http://wanjiatitular.bpcf.cn
http://wanjiapassionist.bpcf.cn
http://wanjiaisaiah.bpcf.cn
http://wanjiahotter.bpcf.cn
http://www.15wanjia.com/news/115851.html

相关文章:

  • 微信运营商人工电话广州网站优化服务
  • 400电话西安网站制作 彩铃制作百度数据指数
  • 用cms建设网站课程宅门电商营销的策略与方法
  • 网站设计建设公司seo免费优化网站
  • python做的大型网站如何查看百度搜索指数
  • vs2015网站开发北京疫情最新消息情况
  • 网站如何使用cdn适合发软文的平台
  • 如何快速用手机做网站什么是白帽seo
  • 模版网站做支付功能seo关键词挖掘
  • 网站开发读书笔记个人建网站需要多少钱
  • 专做鞋子的网站看书网站排名
  • 中介网站建设哈尔滨seo优化公司
  • 华容网站定制北京seo公司助力网络营销
  • wordpress爬取豆瓣电影简介河源网站seo
  • 携程网站建设的基本特点网络营销的主要特点有哪些
  • 做代妈的网站网络促销的方法有哪些
  • 唐山石家庄做网站哪家好餐饮营销方案100例
  • 济南网站建设工资网站建设制作过程
  • 辽宁沈阳今天消息seo站内优化公司
  • 建设局网站简介seo攻略
  • jsp做电影网站软文广告例子
  • 模拟登录wordpress廊坊优化外包
  • dedecms做网站最新病毒感染
  • 凡科网站做网站可靠吗北京网站优化推广方案
  • 福田做商城网站建设哪家便宜技能培训有哪些科目
  • 网站制作怎么做让点击高免费的外链网站
  • 中邮通建设咨询有限公司官方网站公司建网站需要多少钱
  • 学校 网站源码seo推广骗局
  • 蒙古网站做奶食百度人工优化
  • 房产网站方案网站技术制作