Why my code get runtime error for test case {2, 1}


  • 0
    W

    Here is my code, I got runtime error in the leetcode online judge. But I ran it on my local computer for the case above, there is no error and everything seems right. Could anybody tell me what I did wrong here? I just implement the quick sort in the linklist. Thanks ahead!

    class Solution {
    public:
        ListNode *sortList(ListNode *head) {
            return recur(head, NULL);
        }
    private:
        ListNode *recur(ListNode *head, ListNode *tail) {
            if (head == tail) return NULL;
            if (head -> next == tail) return head;
            ListNode *less, *more, *scanL, *scanM, *pivot = head;
            head = head -> next;
            while (head != tail) {
                if (head -> val <= pivot -> val)
                    if (less == NULL) {
                        less = head;
                        scanL = head;
                    } else {
                        scanL -> next = head;
                        scanL = head;
                    }
                    else
                        if (more == NULL) {
                            more = head;
                            scanM = head;
                        } else {
                            scanM -> next = head;
                            scanM = head;
                        }
                head = head -> next;
            }
            if (less) {
                scanL -> next = pivot;
                less = recur(less, pivot);
            } else less = pivot;
            if (more) {
                scanM -> next = tail;
                pivot -> next = recur(more, tail);
            } else pivot -> next = tail;
            return less;
        }
    };

  • 0

    I also use quicksort to sove this problem,but cannot solve it,and meet the same question with
    you,can you solve it with this way???


Log in to reply
 

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