码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode148. 排序链表


    给你链表的头结点 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 = []
    输出:[]

    思路:使用快慢指针,截断节点

    class Solution {
        public ListNode sortList(ListNode head) {
            if (head == null) {
                return head;
            }
            if (head.next == null) {
                return head;
            }
    
            ListNode slow = head;
            ListNode quick = head;
    
            while (quick.next != null && quick.next.next != null) {
                slow = slow.next;
                quick = quick.next.next;
            }
    
            ListNode next = slow.next;
            slow.next = null;
    
            ListNode l1 = sortList(head);
            ListNode l2 =sortList(next);
            ListNode listNode = mergeTwoLists(l1, l2);
    
            return listNode;
    
        }
    
        public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
            ListNode head = new ListNode(1);
            ListNode cur = head;
            ListNode cur1 = list1;
            ListNode cur2 = list2;
            while (cur1 != null && cur2 != null) {
                if (cur1.val > cur2.val) {
                    cur.next = new ListNode(cur2.val);
                    cur = cur.next;
                    cur2 = cur2.next;
                } else if (cur1.val < cur2.val) {
                    cur.next = new ListNode(cur1.val);
                    cur = cur.next;
                    cur1 = cur1.next;
                } else {
                    cur.next = new ListNode(cur1.val);
                    cur.next.next = new ListNode(cur2.val);
                    cur = cur.next.next;
                    cur1 = cur1.next;
                    cur2 = cur2.next;
                }
            }
            while (cur1 != null) {
                cur.next = cur1;
                cur = cur.next;
                cur1 = cur1.next;
            }
    
            while (cur2 != null) {
                cur.next = cur2;
                cur = cur.next;
                cur2 = cur2.next;
            }
            if (head.next != null) {
                head = head.next;
            } else {
                head = null;
            }
    
            return head;
        }
    }
    
  • 相关阅读:
    Rochchip Gststreamer 硬件编解码pipeline
    安恒信息明御安全网关 suffix参数任意文件上传漏洞
    开放签电子签章企业版V1.6(紧急更新)
    C++模板与STL(三):容器与算法
    软考系统架构师倒计时第1天
    人力资源管理软件让每位员工的记录触手可及
    电商API接口解析及操作案例
    聊聊大模型的微调实现及其应用
    HTTP 连接详解
    低代码平台协同OA升级,促进金融企业信息化建设
  • 原文地址:https://blog.csdn.net/liuhongtaowxp1/article/details/125993089
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号