class Solution: def mergeKLists(self , lists: List[ListNode]) -> ListNode: # write code here n=lists.__len__() if n==0: return None elif n==1: return lists[0] elif n==2: pHead1=lists[0] pHead2=lists[1] if pHead1==None: return pHead2 elif pHead2==None: return pHead1 if pHead1.val<=pHead2.val: p1=pHead1 p2=pHead2 p3=p1 else: p1=pHead2 p2=pHead1 p3=p1 while p2!=None and p1.next!=None: if p2.val<p1.next.val: p2.next,p1.next,p2=p1.next,p2,p2.next p1=p1.next if p1.next==None: p1.next=p2 return p3 else: lists2=[self.mergeKLists(lists[0:(n//2)]),self.mergeKLists(lists[(n//2):n])] return self.mergeKLists(lists2)