• 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. }

  • 相关阅读:
    npm详解
    coredns无法启动
    Java使用lowagie根据模版动态生成PDF(无需额外设置系统字体)
    大数据:Trino简介及ETL场景的解决方案
    Vue--1.7watch侦听器(监视器)
    4.springboot中整合Mybatis
    【计算机网络】网络原理
    微服务项目:尚融宝(40)(核心业务流程:申请借款额度(3))
    CSS 计数器之 counter()
    VectorDraw Developer Framework 10.1004 Crack
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126220508