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

什么类型的产品可以做网站出口关键词研究工具

什么类型的产品可以做网站出口,关键词研究工具,自助餐火锅网站建设,教育中介公司网站建设费用148. 排序链表 题目:给你链表的头结点 head ,请将其按升序排列并返回排序后的链表。 题目链接: 148. 排序链表 时间复杂度:快排 O(n^2) 超出时间限制 class Solution {public ListNode sortList(ListNode head) {if(headnull)…

148. 排序链表

题目:给你链表的头结点 head ,请将其按升序排列并返回排序后的链表。
在这里插入图片描述

题目链接: 148. 排序链表
时间复杂度:快排 O(n^2) 超出时间限制

class Solution {public ListNode sortList(ListNode head) {if(head==null){return head;}ListNode dummy=new ListNode(Integer.MIN_VALUE,null);ListNode pointnew=dummy;ListNode pointold=head;while(pointold!=null){while(pointnew!=null&&pointnew.next!=null){if(pointold.val<=pointnew.next.val){ListNode next=pointnew.next;ListNode node=new ListNode(pointold.val);pointnew.next=node;node.next=next;pointnew=dummy;break;}else{pointnew=pointnew.next;}}if(pointnew.next==null){ListNode next=pointnew.next;ListNode node=new ListNode(pointold.val);pointnew.next=node;node.next=next;pointnew=dummy;}pointold=pointold.next;}return dummy.next;}
}

归并排序O(logn):

class Solution {public ListNode sortList(ListNode head) {if(head==null||head.next==null){return head;}//找中点截断链表ListNode fast = head;ListNode slow = head;ListNode pre=null;while(fast!=null&&fast.next!=null){pre=slow;slow=slow.next;fast=fast.next.next;}//递归截断链表pre.next=null;ListNode left=sortList(head);ListNode right=sortList(slow);//合并链表ListNode dummy=new ListNode(0);ListNode res = dummy;while (left != null && right != null) {if (left.val < right.val) {res.next = left;left = left.next;} else {res.next = right;right = right.next;}res=res.next;}res.next=left!=null?left:right;return dummy.next;}
}

归并排序迭代方法 时间复杂度O(logn),空间复杂度为O(1):
直接当作n个长度为1的链表进行归并 先归并为2个有序,继而4,8…直到其长度大于链表长度n

public ListNode sortList(ListNode head) {if (head == null || head.next == null) {return head;}// 获取链表长度int length = 0;ListNode current = head;while (current != null) {length++;current = current.next;}ListNode dummy = new ListNode(0);dummy.next = head;ListNode left, right, tail;// 每次翻倍增加子链表的长度for (int step = 1; step < length; step *= 2) {current = dummy.next;tail = dummy;while (current != null) {left = current;right = split(left, step); // 分割出两个子链表current = split(right, step); //划分下一个lefttail = merge(left, right, tail); // 合并两个子链表}}return dummy.next;}// 分割链表private ListNode split(ListNode head, int step) {if (head == null) return null;for (int i = 1; head.next != null && i < step; i++) {head = head.next;}ListNode right = head.next;head.next = null;return right;}// 合并两个链表private ListNode merge(ListNode l1, ListNode l2, ListNode tail) {ListNode current = tail;while (l1 != null && l2 != null) {if (l1.val < l2.val) {current.next = l1;l1 = l1.next;} else {current.next = l2;l2 = l2.next;}current = current.next;}current.next = (l1 != null) ? l1 : l2;while (current.next != null) {current = current.next;}return current;}
http://www.yidumall.com/news/16997.html

相关文章:

  • 一个网站用多少个css样式表最全bt搜索引擎入口
  • 网站高速下载如何做上海seo优化公司
  • 哪个网站做数学题赚钱电商平台的推广及运营思路
  • 建设项目立项网站优化营商环境存在问题及整改措施
  • 阿里云网站建设如何无锡网站服务公司
  • wordpress goseo关键词怎么选
  • 什么网站可以做图赚钱seo网站优化培训班
  • 郑州 网站开发百度推广要多少钱
  • 做网站的会计分录石家庄网站建设案例
  • 网站怎么做h5支付宝支付接口关于搜索引擎的搜索技巧
  • 请人做网站 我需要知道哪几点福州短视频seo服务
  • 搜狐自助建站哪个平台好用seo的外链平台有哪些
  • 百度主机做视频网站怎么样竞价外包运营
  • 微博营销网站源码安徽网站推广
  • mvc5网站开发实战详解东莞网络推广招聘
  • 昆山网页网站制作2023第三波疫情已经到来了
  • 网站开发对显卡的要求网络营销策划师
  • 高端网站设计服务商如何给网站做推广
  • 济南微网站建设列举网络推广的方式
  • 中央建设的未成年专属网站附近有没有学电脑培训的
  • 做推送网站描述建设一个网站的具体步骤
  • wordpress二次开发教程四川企业seo
  • Wordpress标签与分类百度seo
  • 外贸网站建设青岛免费优化
  • 论坛网站 备案关键词seo排名怎么选
  • 成都市网站建设公司石家庄网站建设方案
  • 网站建设需要提供的资料文档网址大全浏览器
  • 网站产品链接怎么做的免费b站推广网站入口
  • 网页制作初体验教案seo官网优化怎么做
  • 网站更新内容国外友链买卖平台