LeetCode148. ๆŽ’ๅบ้“พ่กจ๐ŸŒŸ๐ŸŒŸไธญ็ญ‰

่ฏพๅŽไฝœไธš

้—ฎ้ข˜ๆ่ฟฐ

ๅŽŸๆ–‡้“พๆŽฅ๏ผš148. ๆŽ’ๅบ้“พ่กจ

็ป™ไฝ ้“พ่กจ็š„ๅคด็ป“็‚น head ๏ผŒ่ฏทๅฐ†ๅ…ถๆŒ‰ๅ‡ๅบๆŽ’ๅˆ—ๅนถ่ฟ”ๅ›žๆŽ’ๅบๅŽ็š„้“พ่กจ ใ€‚

็คบไพ‹ 1๏ผš

img

่พ“ๅ…ฅ๏ผšhead = [4,2,1,3]
่พ“ๅ‡บ๏ผš[1,2,3,4]

็คบไพ‹ 2๏ผš

img

่พ“ๅ…ฅ๏ผšhead = [-1,5,3,4,0]
่พ“ๅ‡บ๏ผš[-1,0,3,4,5]

็คบไพ‹ 3๏ผš

่พ“ๅ…ฅ๏ผšhead = []
่พ“ๅ‡บ๏ผš[]

ๆ็คบ๏ผš

  • ้“พ่กจไธญ่Š‚็‚น็š„ๆ•ฐ็›ฎๅœจ่Œƒๅ›ด [0, 5 * 104] ๅ†…
  • -105 <= Node.val <= 105

ไปฃ็ ๅฎž็Žฐ

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        // ้€’ๅฝ’็ป“ๆŸๆกไปถ
        if(head == null || head.next == null){
            return head;
        }
        // ๅ…ˆๆ‰พๅˆฐไธญ้—ด่Š‚็‚น
        ListNode mid = middleNode(head);
        // ๆ‹†ๅˆ†ๅทฆๅณไธคไธช้“พๆก
        ListNode left = head;
        ListNode right = mid.next;
        mid.next = null;
        // ไธค่พน้€’ๅฝ’ๆŽ’ๅบ
        ListNode lHead = sortList(left);
        ListNode rHead = sortList(right);
        // ๅˆๅนถๆœ‰ๅบ้“พ่กจ
        ListNode mergeHead = mergeTwoLists(lHead, rHead);

        return mergeHead;

    }



    // ่ฟ”ๅ›ž้“พ่กจไธญ้—ด่Š‚็‚น๏ผŒๅฆ‚ๆžœ่Š‚็‚นไธชๆ•ฐๆ˜ฏๅถๆ•ฐ๏ผŒๅˆ™่ฟ”ๅ›ž็ฌฌไธ€ไธชไธญ้—ด่Š‚็‚น
    public ListNode middleNode(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }

        ListNode slow = head;
        ListNode fast = head;

        while(fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;

    }
    // ๅˆๅนถไธคไธชๆœ‰ๅบ็š„้“พ่กจ
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode list3 = new ListNode(-1);
        ListNode temp = list3;
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val){
               temp.next = list1;
               list1 = list1.next;

            } else {
               temp.next = list2;
               list2 = list2.next;
            }
            temp = temp.next;
        }
        temp.next = list1 == null ? list2 : list1;
        return list3.next;
    }
}

Python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution(object):
    def sortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        # ้€’ๅฝ’็ป“ๆŸๆกไปถ
        if head == None or head.next == None:
            return head

        # ๅ…ˆๆ‰พๅˆฐไธญ้—ด่Š‚็‚น
        mid = self.middleNode(head)

        # ๆ‹†ๅˆ†ๅทฆๅณไธคไธช้“พๆก
        left = head
        right = mid.next
        mid.next = None

        # ไธค่พน้€’ๅฝ’ๆŽ’ๅบ
        lHead = self.sortList(left)
        rHead = self.sortList(right)

        # ๅˆๅนถๆœ‰ๅบ้“พ่กจ
        mergeHead = self.mergeTwoLists(lHead, rHead)

        return mergeHead


    # ่ฟ”ๅ›ž้“พ่กจไธญ้—ด่Š‚็‚น๏ผŒๅฆ‚ๆžœ่Š‚็‚นไธชๆ•ฐๆ˜ฏๅถๆ•ฐ๏ผŒๅˆ™่ฟ”ๅ›ž็ฌฌไธ€ไธชไธญ้—ด่Š‚็‚น
    def middleNode(self, head):
        if head == None or head.next == None:
            return head
        slow = head
        fast = head
        while fast.next != None and fast.next.next != None:
            fast = fast.next.next
            slow = slow.next
        return slow

    # ๅˆๅนถไธคไธชๆœ‰ๅบ็š„้“พ่กจ
    def mergeTwoLists(self, list1, list2):
        list3 = ListNode(-1)
        temp = list3
        while list1 != None and list2 != None:
            if list1.val <= list2.val:
                temp.next = list1
                list1 = list1.next
            else:
                temp.next = list2
                list2 = list2.next
            temp = temp.next
        temp.next = list1 if list1 != None else list2
        return list3.next

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        // ้€’ๅฝ’็ป“ๆŸๆกไปถ
        if (head == nullptr || head->next == nullptr) {
            return head;
        }

        // ๅ…ˆๆ‰พๅˆฐไธญ้—ด่Š‚็‚น
        ListNode* mid = middleNode(head);

        // ๆ‹†ๅˆ†ๅทฆๅณไธคไธช้“พๆก
        ListNode* left = head;
        ListNode* right = mid->next;
        mid->next = nullptr;

        // ไธค่พน้€’ๅฝ’ๆŽ’ๅบ
        ListNode* lHead = sortList(left);
        ListNode* rHead = sortList(right);

        // ๅˆๅนถๆœ‰ๅบ้“พ่กจ
        ListNode* mergeHead = mergeTwoLists(lHead, rHead);

        return mergeHead;
    }

    // ่ฟ”ๅ›ž้“พ่กจไธญ้—ด่Š‚็‚น๏ผŒๅฆ‚ๆžœ่Š‚็‚นไธชๆ•ฐๆ˜ฏๅถๆ•ฐ๏ผŒๅˆ™่ฟ”ๅ›ž็ฌฌไธ€ไธชไธญ้—ด่Š‚็‚น
    ListNode* middleNode(ListNode* head) {
        if (head == nullptr || head->next == nullptr) {
            return head;
        }

        ListNode* slow = head;
        ListNode* fast = head;

        while (fast->next != nullptr && fast->next->next != nullptr) {
            fast = fast->next->next;
            slow = slow->next;
        }

        return slow;
    }

    // ๅˆๅนถไธคไธชๆœ‰ๅบ็š„้“พ่กจ
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode* list3 = new ListNode(-1);
        ListNode* temp = list3;

        while (list1 != nullptr && list2 != nullptr) {
            if (list1->val <= list2->val) {
                temp->next = list1;
                list1 = list1->next;

            } else {
                temp->next = list2;
                list2 = list2->next;
            }
            temp = temp->next;
        }

        temp->next = list1 == nullptr ? list2 : list1;
        return list3->next;
    }
};

Go

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func sortList(head *ListNode) *ListNode {
    // ้€’ๅฝ’็ป“ๆŸๆกไปถ
    if head == nil || head.Next == nil {
        return head
    }

    // ๅ…ˆๆ‰พๅˆฐไธญ้—ด่Š‚็‚น
    mid := middleNode(head)

    // ๆ‹†ๅˆ†ๅทฆๅณไธคไธช้“พๆก
    left := head
    right := mid.Next
    mid.Next = nil

    // ไธค่พน้€’ๅฝ’ๆŽ’ๅบ
    lHead := sortList(left)
    rHead := sortList(right)

    // ๅˆๅนถๆœ‰ๅบ้“พ่กจ
    mergeHead := mergeTwoLists(lHead, rHead)

    return mergeHead
}

// ่ฟ”ๅ›ž้“พ่กจไธญ้—ด่Š‚็‚น๏ผŒๅฆ‚ๆžœ่Š‚็‚นไธชๆ•ฐๆ˜ฏๅถๆ•ฐ๏ผŒๅˆ™่ฟ”ๅ›ž็ฌฌไธ€ไธชไธญ้—ด่Š‚็‚น
func middleNode(head *ListNode) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }

    slow := head
    fast := head

    for fast.Next != nil && fast.Next.Next != nil {
        fast = fast.Next.Next
        slow = slow.Next
    }

    return slow
}

// ๅˆๅนถไธคไธชๆœ‰ๅบ็š„้“พ่กจ
func mergeTwoLists(list1, list2 *ListNode) *ListNode {
    list3 := &ListNode{-1, nil}
    temp := list3

    for list1 != nil && list2 != nil {
        if list1.Val <= list2.Val {
            temp.Next = list1
            list1 = list1.Next
        } else {
            temp.Next = list2
            list2 = list2.Next
        }
        temp = temp.Next
    }

    if list1 == nil {
        temp.Next = list2
    } else {
        temp.Next = list1
    }

    return list3.Next
}

ๅ‘่กจ่ฏ„่ฎบ

ๅŽๆ‰่ƒฝ่ฏ„่ฎบ