I am relatively new to python,..can someone tell me why I get the runtime error in this code?


  • 0
    A
    class Solution:
        def sortList(self, head):
           self.head = head
           length = self.length_of_linkedlist(self.head)
        
           if length > 1:
            length = length // 2
            mid = self.new_mid(self.head, length)
            self.sortList(self.head)
            self.sortList(mid)
            
            i,j,k = self.head,mid,self.head
            while i< self.length_of_linkedlist(self.head) and j < self.length_of_linkedlist(mid):
                if i.val < j.val:
                    k = i
                    i = i.next
                else:
                    k = j 
                    j = j.next
                
        
            while i < self.length_of_linkedlist(self.head):
                k = i.
                i = i.next
        
            while j < self.length_of_linkedlist(self.head):
                k = j
                j = j.next
        
                self.head = k
            return self.head
            
    
    def length_of_linkedlist(self, list_head):
        self.list_head = list_head
        count = 0
        while self.list_head != None:
            self.list_head = self.list_head.next
            count += 1
        return count
        
    def new_mid(self, list_head, length):
        self.list_head = list_head
        self.length = length
        count = 0
        while count != self.length:
            self.list_head = self.list_head.next
            count += 1 
        return self.list_head

Log in to reply
 

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