Is this a cheating way?


  • 0
    X
    class Solution {
    public:
    ListNode* sortList(ListNode* head) {
        vector<int> nums;
        ListNode* p=head;
        while(p)
        {
            nums.push_back(p->val);
            p=p->next;
        }
        
        sort(nums.begin(),nums.end());
        int i=0;
        p=head;
        while(p)
        {
            p->val=nums[i];
            p=p->next;
            i++;
        }
        return head;
    }
    

    };


  • 0
    J

    sure i think the test case does not justify space used. Absolutely it's not the constant space solution


  • 0
    X

    of course. The purpose of this question is to make a O(nlogn) time complexity, O(n) space sort, which means heap sort.


Log in to reply
 

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