SortList Python TLE

• I think the following code is O(nlogn), however the system keep throwing TLE for large input.

``````class Solution:
def merge(self, left, right):
while left != None and right != None:
if left.val <= right.val:
v = left
left = left.next
else:
v = right
right = right.next

curr.next = v
curr = curr.next

while left != None:
curr.next = left
left = left.next
curr = curr.next

while right != None:
curr.next =right
right = right.next
curr = curr.next

curr.next = None

# @return a ListNode

while right.next != None and right.next.next != None:
left = left.next
right = right.next.next

right = left.next
left.next = None

left = self.sortList(left)
right = self.sortList(right)

return self.merge(left, right)``````

• I think it will be better if you replace the last two while loop of merge() with if ,which is as follows,

`````` def merge(self, left, right):
while left != None and right != None:
if left.val <= right.val:
curr.next = left
left = left.next
else:
curr.next = right
right = right.next

curr = curr.next
if left != None:
curr.next = left

if right != None:
curr.next = right