Why TLE for this solution?


  • 0

    this code runs fine in my computer,but i got TLE ,who can tell me why? thanks

    public ListNode insertionSortList(ListNode head) {
    if(head == null || head.next == null){
        return head;
    }
    
    ListNode sorted = head;
    
    ListNode temp ;
    
    ListNode cursor;
    
    while(sorted.next!=null){
    
        temp = sorted.next;
    
        cursor = head;
    
        if(temp.val < cursor.val){
    
            sorted.next = temp.next;
            temp.next = cursor.next;
            cursor.next = temp;
            head = temp;
            continue;
    
        }
    
        while(cursor.next!=null && cursor!=sorted){
            if(cursor.next.val > temp.val){
                sorted.next = temp.next;
                temp.next = cursor.next;
                cursor.next = temp;
                break;
            }
            cursor=cursor.next;
        }
        if(cursor == sorted){
            sorted = sorted.next;
        }
    }
    
    return head;
    

    }


  • 0
    S

    Double check the 'if(temp.val < cursor.val)' block. It does not remove/insert listnode in the correct way. Try to check the code with a simple input {5, 4} and you will know what I mean.


Log in to reply
 

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