!~..~! possible bug point


  • 0

    I plan to share all my AC code after no more than 3 times submitting and sharing the possible bug points .
    I will mark !~..~! at All the bugs I made .

    class Solution {
        public:
            ListNode* sortList(ListNode* head) {
                if(head==NULL || head->next==NULL)  return head;
                // !~..~!  
                ListNode *mid=head, *tail=head->next;
                while(tail && tail->next) { mid=mid->next; tail=tail->next->next; }
                // !~..~!
                ListNode *left=head, *right=mid->next;
                mid->next=NULL;
                ListNode* sorted_left = sortList(left);
                ListNode* sorted_right = sortList(right);
                return _merge(sorted_left, sorted_right);
            }
            
            ListNode* _merge(ListNode* left, ListNode* right){
                ListNode* result=new ListNode(-1);
                ListNode* cur=result;
                while(left && right){
                    if(left->val < right->val)  { 
                        cur->next=left; cur=cur->next; left=left->next; 
                    }
                    else    {                
                        cur->next=right; cur=cur->next; right=right->next;
                    }
                }
                if(left)    cur->next=left;
                if(right)   cur->next=right;
                return result->next;
            }
        };

Log in to reply
 

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