LeetCode148. ๆๅบ้พ่กจ๐๐ไธญ็ญ
่ฏพๅไฝไธ
้ฎ้ขๆ่ฟฐ
ๅๆ้พๆฅ๏ผ148. ๆๅบ้พ่กจ
็ปไฝ ้พ่กจ็ๅคด็ป็น 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 = []
่พๅบ๏ผ[]
ๆ็คบ๏ผ
- ้พ่กจไธญ่็น็ๆฐ็ฎๅจ่ๅด
[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
}