码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode-148. Sort List [C++][Java]


    LeetCode-148. Sort List Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/sort-list/

    Given the head of a linked list, return the list after sorting it in ascending order.

    Example 1:

    Input: head = [4,2,1,3]
    Output: [1,2,3,4]
    

    Example 2:

    Input: head = [-1,5,3,4,0]
    Output: [-1,0,3,4,5]
    

    Example 3:

    Input: head = []
    Output: []
    

    Constraints:

    • The number of nodes in the list is in the range [0, 5 * 10^4].
    • -10^5 <= Node.val <= 10^5

    Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?

    【C++】

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode() : val(0), next(nullptr) {}
    7. * ListNode(int x) : val(x), next(nullptr) {}
    8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
    9. * };
    10. */
    11. class Solution {
    12. public:
    13. ListNode* sortList(ListNode* head) {
    14. if(!head || !head->next) {return head;}
    15. ListNode *mid = nullptr, *slow = head, *fast = head;
    16. while(fast && fast->next) {
    17. mid = slow;
    18. slow = slow->next;
    19. fast = fast->next->next;
    20. }
    21. mid->next = nullptr;
    22. ListNode* l1 = sortList(head);
    23. ListNode* l2 = sortList(slow);
    24. return mergelist(l1, l2);
    25. }
    26. ListNode* mergelist(ListNode *l1, ListNode *l2) {
    27. ListNode *dummy = new ListNode(0), *curr = dummy;
    28. while (l1 && l2) {
    29. if (l1->val > l2->val) {
    30. curr->next = l2;
    31. l2 = l2->next;
    32. } else {
    33. curr->next = l1;
    34. l1 = l1->next;
    35. }
    36. curr = curr->next;
    37. }
    38. if(l1) {
    39. curr->next = l1;
    40. l1 = l1->next;
    41. }
    42. if(l2) {
    43. curr->next = l2;
    44. l2 = l2->next;
    45. }
    46. return dummy->next;
    47. }
    48. };

    【Java】

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. public ListNode sortList(ListNode head) {
    13. if(head == null || head.next == null) {return head;}
    14. ListNode mid = null, slow = head, fast = head;
    15. while (fast != null && fast.next != null) {
    16. mid = slow;
    17. slow = slow.next;
    18. fast = fast.next.next;
    19. }
    20. mid.next = null;
    21. ListNode l1 = sortList(head);
    22. ListNode l2 = sortList(slow);
    23. return mergelist(l1, l2);
    24. }
    25. ListNode mergelist(ListNode l1, ListNode l2) {
    26. ListNode dummy = new ListNode(0), curr = dummy;
    27. while (l1 != null && l2 != null) {
    28. if (l1.val > l2.val) {
    29. curr.next = l2;
    30. l2 = l2.next;
    31. } else {
    32. curr.next = l1;
    33. l1 = l1.next;
    34. }
    35. curr = curr.next;
    36. }
    37. if(l1 != null) {
    38. curr.next = l1;
    39. l1 = l1.next;
    40. }
    41. if(l2 != null) {
    42. curr.next = l2;
    43. l2 = l2.next;
    44. }
    45. return dummy.next;
    46. }
    47. }

  • 相关阅读:
    C/S架构学习之多线程实现TCP并发服务器
    某金融机构身份国产化LDAP创新实践——国产自主可控 LDAP目录服务建设经验分享
    (数据结构与算法)LeetCode刷题笔记2-0005:最长回文子串
    如何用思维导图做笔记
    带你快速概览MySQL 整体架构
    设备接入服务组件->微服务and容器化改造说明文档
    SpringBoot + Vue 实现侧边栏目录动态展示
    模糊控制器实现对某个对象追踪输入
    mysql57开启biglog并查看biglog保姆级教程
    【Android入门】6、ContentProvider:跨程序的数据共享:访问其他 App、被其他 App 访问
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126220508
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号