input: Write a python function to sort a linked list.
  assert sort_linked_list(ListNode.from_list([4, 2, 1, 3])).to_list() == [1, 2, 3, 4]
  assert sort_linked_list(ListNode.from_list([10, 3, 5, 2, 7])).to_list() == [2, 3, 5, 7, 10]
  assert sort_linked_list(ListNode.from_list([3, 1])).to_list() == [1, 3]
output: class ListNode:
      def __init__(self, val=0, next=None):
          self.val = val
          self.next = next
  
      @staticmethod
      def from_list(lst):
          if not lst:
              return None
          head = ListNode(lst[0])
          current = head
          for val in lst[1:]:
              current.next = ListNode(val)
              current = current.next
          return head
  
      def to_list(self):
          result = []
          current = self
          while current:
              result.append(current.val)
              current = current.next
          return result
  
  def sort_linked_list(head):
      if not head or not head.next:
          return head
  
      def get_middle(node):
          slow, fast = node, node
          while fast.next and fast.next.next:
              slow = slow.next
              fast = fast.next.next
          return slow
  
      def merge(l1, l2):
          dummy = ListNode()
          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
  
      middle = get_middle(head)
      next_to_middle = middle.next
      middle.next = None
  
      left = sort_linked_list(head)
      right = sort_linked_list(next_to_middle)
  
      sorted_list = merge(left, right)
      return sorted_list