sort of clean merge sort, python


  • 0
    S
    def sortList(self, head):
    	if not head or not head.next:
    		return head
    	slow = head
    	fast = head
    	while fast.next and fast.next.next:
    		fast = fast.next.next
    		slow = slow.next
    	B = self.sortList(slow.next)
    	slow.next = None
    	A = self.sortList(head)
    	dummy = ListNode(0)
    	M = dummy
    	while A and B:
    		if A.val <= B.val:
    			M.next = A
    			A = A.next
    		else:
    			M.next = B
    			B = B.next
    		M = M.next
    	if A:
    		M.next = A
    	elif B:
    		M.next = B
    	return dummy.next
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.