王廷瑋|數位醫療|智慧醫療: 148. Sort List WFU

2024年7月7日 星期日

148. Sort List

148. Sort List


給定一個鏈表的頭節點,返回排序後的鏈表,按升序排列。

範例 :

輸入: head = [4,2,1,3] 輸出: [1,2,3,4]


Python


from typing import Optional

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

class Solution:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
# Function to split the linked list into two halves
def split(head: ListNode) -> ListNode:
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
mid = slow.next
slow.next = None
return mid
# Function to merge two sorted linked lists
def merge(l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode(0)
tail = dummy
while l1 and l2:
if l1.val < l2.val:
tail.next = l1
l1 = l1.next
else:
tail.next = l2
l2 = l2.next
tail = tail.next
tail.next = l1 if l1 else l2
return dummy.next
# Split the list into two halves
mid = split(head)
# Recursively sort each half
left = self.sortList(head)
right = self.sortList(mid)
# Merge the two sorted halves
return merge(left, right)

32.50MB, 395ms


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 || !head->next) {
return head;
}

// Split the list into two halves
ListNode* mid = split(head);

// Recursively sort each half
ListNode* left = sortList(head);
ListNode* right = sortList(mid);

// Merge the two sorted halves
return merge(left, right);
}

private:
ListNode* split(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
ListNode* mid = slow->next;
slow->next = nullptr;
return mid;
}

ListNode* merge(ListNode* l1, ListNode* l2) {
ListNode dummy(0);
ListNode* tail = &dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
tail->next = l1;
l1 = l1->next;
} else {
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
tail->next = l1 ? l1 : l2;
return dummy.next;
}
};

54.50MB, 114ms


Javascript


/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val === undefined ? 0 : val);
* this.next = (next === undefined ? null : next);
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var sortList = function(head) {
if (!head || !head.next) {
return head;
}

// Function to split the linked list into two halves
const split = (head) => {
let slow = head;
let fast = head.next;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
const mid = slow.next;
slow.next = null;
return mid;
};

// Function to merge two sorted linked lists
const merge = (l1, l2) => {
const dummy = new ListNode(0);
let tail = dummy;
while (l1 && l2) {
if (l1.val < l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = l1 || l2;
return dummy.next;
};

// Split the list into two halves
const mid = split(head);

// Recursively sort each half
const left = sortList(head);
const right = sortList(mid);

// Merge the two sorted halves
return merge(left, right);
};

71.37MB, 244ms