• LeetCode //C - 148. Sort List


    148. 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 ∗ 1 0 4 0, 5 * 10^4 0,5104].
    • − 1 0 5 < = N o d e . v a l < = 1 0 5 -10^5 <= Node.val <= 10^5 105<=Node.val<=105

    From: LeetCode
    Link: 148. Sort List


    Solution:

    Ideas:

    1. Divide the List: The main idea is to split the list into two halves, sort each half, and then merge the sorted halves back together. This is a recursive approach.

    2. Find the Middle:

    • findMiddle function is used to get the middle node of the list. This is done using the slow and fast pointer approach (sometimes called the hare and tortoise approach). The fast pointer moves two steps at a time, and the slow pointer moves one step at a time. When the fast pointer reaches the end of the list, the slow pointer is at the middle.
    • After finding the middle, the list is split into two by updating the next pointer of the node before the middle to NULL, effectively creating two separate lists.

    3. Recursive Sort:

    • The sortList function calls itself recursively on each half of the list until it reaches a base case where the list is empty or has only one node (which is inherently sorted).
    • Once the base case is reached, the recursion starts to unwind, and the merge process begins.

    4. Merge Sorted Lists:

    • The merge function takes two sorted lists as input and merges them into a single sorted list.
    • A dummy node is used to simplify the merging process. This node doesn’t contain any actual data and is only used as a starting point.
    • The function iterates over the two lists, comparing the current nodes’ values. The smaller node is attached to the current node in the merged list, and the pointer of the list containing that node is moved to the next node.
    • This process continues until one of the lists is exhausted. The remaining nodes from the non-empty list are then attached to the merged list, ensuring all nodes are included in the sorted order.

    5. Completing the Merge:

    • As the recursion unwinds, smaller sorted portions of the list are merged together. This process continues until the entire list is sorted and merged.
    Code:
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* merge(struct ListNode* l1, struct ListNode* l2) {
        struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
        struct ListNode* current = dummy;
        while (l1 && l2) {
            if (l1->val < l2->val) {
                current->next = l1;
                l1 = l1->next;
            } else {
                current->next = l2;
                l2 = l2->next;
            }
            current = current->next;a
        }
        if (l1) current->next = l1;
        if (l2) current->next = l2;
        return dummy->next;
    }
    
    struct ListNode* findMiddle(struct ListNode* head) {
        struct ListNode* prev = NULL;
        struct ListNode* slow = head;
        struct ListNode* fast = head;
        while (fast && fast->next) {
            prev = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        if (prev) prev->next = NULL;  // Split the list into two halves
        return slow;
    }
    
    struct ListNode* sortList(struct ListNode* head) {
        if (!head || !head->next) return head; // Base case: if the list is empty or has only one node
        struct ListNode* mid = findMiddle(head);
        struct ListNode* left = sortList(head);
        struct ListNode* right = sortList(mid);
        return merge(left, right);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    AUTOSAR规范与ECU软件开发(实践篇)9.4 AUTOSAR安全机制的存储空间分区
    基于点的数据分析
    Git在已有的项目中引入Submodule子模块管理:添加、更新、删除(实战示例代码)
    图片公式识别@文档公式识别@表格识别@在线和离线OCR工具
    Upgrade flutter2.1 to flutter3.0
    DevUI开源经验分享:从0到1开始运营你的开源项目
    java框架-Springboot3-数据访问
    Java常用类,这一次帮你总结好
    Chapter8_FundamentalsOfComputerGraphic
    【域泛化】2022 IJCAI领域泛化教程报告
  • 原文地址:https://blog.csdn.net/navicheung/article/details/133850805