# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defsortList(self, head: Optional[ListNode]) -> Optional[ListNode]: ifnot head ornot head.next: return head slow, fast = head,head.next while fast and fast.next: slow = slow.next fast = fast.next.next mid, slow.next = slow.next, None
left, right = self.sortList(head), self.sortList(mid) h = res=ListNode(0) while left and right: if left.val <= right.val: h.next,left = left, left.next else: h.next, right = right, right.next h = h.next