Why the exceed the time? Please help!


  • 0
    G

    class Solution {
    public:
    ListNode *sortList(ListNode *head) {
    ListNode *first=head, *second;
    if(first!=NULL){

        while(first->next!=NULL)
        {   second=first->next;
            while(second!=NULL){
                if(first->val>second->val){
                    int k=first->val;
                    first->val=second->val;
                    second->val=k;
                }
                second=second->next;
            }
                first=first->next;
        }
        }
        return head;
         
    }
    

    };


  • 1
    T

    which sort algorithm you are using?
    If the time complexity of this algorithm is beyond O(nlogn), this may not a good solution.


Log in to reply
 

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