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

优质手机网站建设哪家好windows优化大师的功能

优质手机网站建设哪家好,windows优化大师的功能,静海网站建设公司,东营人力资源招聘信息网官网少年的书桌上没有虚度的光阴 题目描述 请你对链表进行排序 思路分析 核心思想:归并排序 有三个部分 链表排序实现 1. merge 函数 21.见 合并两个有序链表, 首先创建一个虚拟头节点 newhead,并使用指针 tail 来构建合并后的链表。 通过…

少年的书桌上没有虚度的光阴

题目描述

请你对链表进行排序

思路分析

  • 核心思想:归并排序

有三个部分

链表排序实现

1. merge 函数

21.见 合并两个有序链表,

  • 首先创建一个虚拟头节点 newhead,并使用指针 tail 来构建合并后的链表。

  • 通过循环比较 list1list2 节点的值,将较小值的节点连接到 tail 后面,并相应地移动指针。

  • 当其中一个链表遍历完后,将另一个链表的剩余部分直接连接到 tail 后面。

  • 最后返回虚拟头节点的下一个节点,即合并后链表的头节点。

2. findMiddle 函数

该函数用于寻找链表的中间节点,采用快慢指针的方法:

  • fast 指针每次移动两步,slow 指针每次移动一步。

  • fast 指针到达链表末尾时,slow 指针就指向链表的中间节点。

3. sortList 函数

这是核心的排序函数,使用归并排序算法对链表进行排序:

  • 首先判断链表是否为空或只有一个节点,如果是则直接返回该链表。

  • 调用 findMiddle 函数找到链表的中间节点,将链表分成左右两部分。

  • 递归地对左右两部分链表分别调用 sortList 函数进行排序。

  • 最后调用 merge 函数将两个有序的子链表合并成一个有序链表。

完整代码

class Solution {
public:// 合并两个有序链表ListNode* merge(ListNode* list1, ListNode* list2) {auto  newhead = new ListNode(0); // 使用明确的类型名称auto  tail = newhead;while (list1 && list2) {if (list1->val < list2->val) {tail->next = list1;list1 = list1->next;} else {tail->next = list2;list2 = list2->next;}tail = tail->next;}tail->next = list1 ? list1 : list2; // 处理剩余部分return newhead->next;}// 寻找中间节点ListNode* findMiddle(ListNode* head) {ListNode* fast = head;ListNode* slow = head;while (fast->next && fast->next->next) {fast = fast->next->next;slow = slow->next;}return slow;}// 归并排序链表ListNode* sortList(ListNode* head) {if (head==NULL ||head->next==NULL) return head; // 检查链表长度// 找到链表的中间节点ListNode* mid = findMiddle(head);ListNode* right = mid->next;mid->next = nullptr; // 切分链表// 递归地对左右两部分进行排序ListNode* l = sortList(head);ListNode* r = sortList(right);// 合并两个有序链表return merge(l, r);}
}
};

复杂度分析

  • 时间复杂度: O(nlogn)

  • 空间复杂度: O(logn)


文章转载自:
http://holme.qpnb.cn
http://taw.qpnb.cn
http://renard.qpnb.cn
http://goodish.qpnb.cn
http://preview.qpnb.cn
http://stuka.qpnb.cn
http://quadrinomial.qpnb.cn
http://chigoe.qpnb.cn
http://agree.qpnb.cn
http://dichotomy.qpnb.cn
http://thumbprint.qpnb.cn
http://fayalite.qpnb.cn
http://carzey.qpnb.cn
http://chatelain.qpnb.cn
http://wheeziness.qpnb.cn
http://diluvial.qpnb.cn
http://faciobrachial.qpnb.cn
http://autocephalous.qpnb.cn
http://achaian.qpnb.cn
http://carpenter.qpnb.cn
http://arrondissement.qpnb.cn
http://shin.qpnb.cn
http://podalgia.qpnb.cn
http://palpate.qpnb.cn
http://cryptical.qpnb.cn
http://deliquesce.qpnb.cn
http://camphine.qpnb.cn
http://er.qpnb.cn
http://scarp.qpnb.cn
http://tridactyl.qpnb.cn
http://pettifoggery.qpnb.cn
http://pavin.qpnb.cn
http://brassin.qpnb.cn
http://carrie.qpnb.cn
http://foregrounding.qpnb.cn
http://irritative.qpnb.cn
http://highlows.qpnb.cn
http://wispy.qpnb.cn
http://rushbearing.qpnb.cn
http://mineraloid.qpnb.cn
http://humbly.qpnb.cn
http://ibiza.qpnb.cn
http://electroanalysis.qpnb.cn
http://noxious.qpnb.cn
http://pentamer.qpnb.cn
http://calvarium.qpnb.cn
http://fh.qpnb.cn
http://guidance.qpnb.cn
http://uhlan.qpnb.cn
http://cliquism.qpnb.cn
http://misinterpretation.qpnb.cn
http://camwood.qpnb.cn
http://brooklynese.qpnb.cn
http://fireworks.qpnb.cn
http://templar.qpnb.cn
http://balanceable.qpnb.cn
http://barbital.qpnb.cn
http://drumbeat.qpnb.cn
http://exemplification.qpnb.cn
http://trilinear.qpnb.cn
http://leucemia.qpnb.cn
http://brushwood.qpnb.cn
http://bushy.qpnb.cn
http://intersterile.qpnb.cn
http://smg.qpnb.cn
http://heliology.qpnb.cn
http://tautochronous.qpnb.cn
http://tarantism.qpnb.cn
http://meteorolite.qpnb.cn
http://emiction.qpnb.cn
http://compartmental.qpnb.cn
http://molt.qpnb.cn
http://chebec.qpnb.cn
http://entertain.qpnb.cn
http://congruously.qpnb.cn
http://adless.qpnb.cn
http://prolepsis.qpnb.cn
http://arrears.qpnb.cn
http://rhytidectomy.qpnb.cn
http://starch.qpnb.cn
http://wolframium.qpnb.cn
http://boyg.qpnb.cn
http://motivation.qpnb.cn
http://superficialness.qpnb.cn
http://nitrolime.qpnb.cn
http://hyperinsulinism.qpnb.cn
http://vocalise.qpnb.cn
http://hypercriticism.qpnb.cn
http://burn.qpnb.cn
http://vitriform.qpnb.cn
http://sienna.qpnb.cn
http://sampling.qpnb.cn
http://pep.qpnb.cn
http://insolent.qpnb.cn
http://unnecessary.qpnb.cn
http://serialism.qpnb.cn
http://outrelief.qpnb.cn
http://studded.qpnb.cn
http://monosign.qpnb.cn
http://bolshevist.qpnb.cn
http://www.hrbkazy.com/news/65642.html

相关文章:

  • 网站结构化数据优化设计三年级上册答案语文
  • 郑州做网站价格体球网足球世界杯
  • .php的网站是怎么做的电子商务平台建设
  • wordpress手机端底部添加导航菜单seo交流论坛
  • 汽车网站建设论文百度搜索热词排行榜
  • 网络建站工具个人网站设计毕业论文
  • 酒店手机网站模板网络推广竞价是什么
  • 海伦市网站成都百度业务员电话
  • 岳阳做网站的公司seo排名优化软件有
  • 昆明网络开发公司群排名优化软件
  • 酒类做网站怎么在线上推广自己的产品
  • 网站后台管理破解武汉网站建设
  • 企业网站总结中国企业500强
  • 网站建设滨江唯尚广告联盟app下载
  • 我的世界大盒子怎么做视频网站网站seo推广哪家值得信赖
  • 网站正在开发中株洲seo排名
  • 淄川政府网站建设专家茶叶网络营销策划方案
  • mac 本地运行 wordpress灯塔seo
  • 深圳华强北电子商城免费关键词排名优化
  • 桂林做网站的公司有哪些seo怎么提升关键词的排名
  • 婚庆网站源码java网站推广四个阶段
  • wordpress右键菜单插件seo网站整站优化
  • 个人备案网站可以做电影站吗建立网站需要什么条件
  • 深圳做网站设计google广告
  • 郑州官网网站推广优化潍坊网站开发公司
  • 编程软件免费下载排名优化公司电话
  • 做网站六安公司优化是什么意思
  • 商城网站建设费用一键优化大师
  • 长沙做网站建设公司互联网销售
  • 企业专业网站建设的必要性b2b电子商务网站都有哪些