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

链表的排序算法

48. 排序链表
给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。

示例 1:
输入:head = [4,2,1,3]
输出:[1,2,3,4]
示例 2:
输入:head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]
示例 3:

输入:head = []
输出:[]

    static ListNode merge(ListNode l1, ListNode l2){ListNode head = new ListNode(-1);ListNode cur = head;while(l1 != null &&  l2 != null){if(l1.val < l2.val){cur.next = l1;l1 = l1.next;}else {cur.next = l2;l2 = l2.next;}cur =cur.next;}if(l1!= null){cur.next = l1;}if(l2 != null){cur.next = l2;}return head.next;}static ListNode sortList02(ListNode head, ListNode tail){if(head == null){return head;}if(head.next == tail){head.next = null;return head;}ListNode fast = head;ListNode slow = head;while(fast != null &&  fast != tail){fast = fast.next;slow = slow.next;if(fast != tail){fast = fast.next;}}ListNode mid = slow;ListNode l1 = sortList02(head, mid);ListNode l2 = sortList02(mid, tail);ListNode sorted = merge(l1,l2);return sorted;}static ListNode sort(ListNode head){ListNode res = sortList02(head, null);return res;}

       寻找链表的中点

         ListNode fast = head;
        ListNode slow = head;
        while(fast != null &&  fast != tail){
            fast = fast.next;
            slow = slow.next;
            if(fast != tail){
                fast = fast.next;
            }
        } 

链表插入排序

 static ListNode sortList(ListNode head) {ListNode cur = head;ListNode newHead = new ListNode(-1);while (cur != null) {ListNode nextNode = cur.next;if (newHead.next == null) {newHead.next = cur;cur.next = null;} else {ListNode p = newHead.next;ListNode pre = newHead;while (p != null && p.val < cur.val) {pre = p;p = p.next;}if (p != null) {cur.next = p;pre.next = cur;}else{pre.next = cur;cur.next = null;}}cur = nextNode;}return newHead.next;}

 138. 随机链表的复制

用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:

val:一个表示 Node.val 的整数。
random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为  null 。
你的代码 只 接受原链表的头节点 head 作为传入参数。

示例 1:

输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

示例 2:

输入:head = [[1,1],[2,1]]
输出:[[1,1],[2,1]]

示例 3:

输入:head = [[3,null],[3,0],[3,null]]
输出:[[3,null],[3,0],[3,null]]
public Node copyRandomList(Node head) {if(head == null){return null;}if(!cached.containsKey(head)){Node node = new Node(head.val);cached.put(head,node);node.next = copyRandomList(head.next);node.random = copyRandomList(head.random);}return cached.get(head);}

这道题挺唬人的,但是只要记住管他随机链表,只要复制节点数据就行

相关文章:

  • 陕西营销型手机网站建设网络营销案例分析ppt
  • js博客网站开发计划书互联网销售可以卖什么产品
  • 东莞网站设计与制作公司seo门户网站
  • 厦门建设厅查询网站今天重大新闻事件
  • 保山企业网站建设网络营销比较好的企业
  • 沈阳优化网站网站优化员seo招聘
  • 医学数据分析实战:冠心病发病因素可视化
  • RGB相机 vs 灰度相机
  • Ubuntu离线安装特定版本的gcc
  • 暴雨来袭,气象卫星能在极端天气预报中起什么作用?
  • 蓝桥杯嵌入式学习(cubemxkeil5)
  • 记一次 Kafka 磁盘被写满的排查经历
  • HarmonyOS Next的HiLog日志系统完全指南:从入门到精通
  • 图的拓扑排序管理 Go 服务启动时的组件初始化顺序
  • Flink内存配置
  • GPU常见规格及算力
  • Langchain实战指南:从入门到精通AI链式编程!
  • 使用 ttrpc 实现高效的进程间通信(附 Go Demo)
  • 从零到一训练一个 0.6B 的 MoE 大语言模型
  • 6月24日星期二今日早报简报微语报早读微语早读
  • 代码随想录|图论|02深度优先搜索理论基础
  • JVM(11)——详解CMS垃圾回收器
  • Excel学习04
  • IAR平台全面升级,提升瑞萨MCU架构的嵌入式软件开发效率
  • 从零开始学习 Go 语言:快速入门指南(完整版)
  • 左神算法之数字字符串解码方案计数算法