## Solution 1 - TLE#

Using a bubble sort approach.

``````class Solution:

def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
not_sorted = True
return
while not_sorted:
not_sorted = False
while tmp.next:
if tmp.val > tmp.next.val:
tmp.val, tmp.next.val  = tmp.next.val, tmp.val
not_sorted = True
tmp = tmp.next
``````

Time Complexity: O(n^2)

## Solution 2#

Using a merge sort approach sort approach.

``````class Solution:

l = 0
while tmp:
l += 1
tmp = tmp.next
return l

if lenn < 2:
mid = lenn // 2
i = 0
while i < mid-1:
l = l.next
i += 1
r = l.next
l.next = None
right = self.merge_sort(r)
return self.merge(left, right)

while tmp:
print("val:", tmp.val)
tmp = tmp.next

def merge(self, left, right):
while left and right:
if left.val > right.val:
tmp.next = ListNode(right.val)
right = right.next
tmp = tmp.next
else:
tmp.next = ListNode(left.val)
left = left.next
tmp = tmp.next
while left:
tmp.next = ListNode(left.val)
left = left.next
tmp = tmp.next
while right:
tmp.next = ListNode(right.val)
right = right.next
tmp = tmp.next