My Accepted C++ code


  • 0
    W
    class Solution {
    public:
        ListNode *insertionSortList(ListNode *head) {
            if(!head||!(head->next)) return head;
            ListNode vn(0), *p,*q,*A,*last;
            vn.next=head;
            p=head;
            while(p->next){
                q=p->next;
                last=&vn;
                A=last->next;
                while(A!=q){
                    if(A->val<=q->val){
                        last->next=A;
                        last=last->next;
                        A=A->next;
                        
                    }else{
                        ListNode *tmp=q->next;
                        last->next=q;
                        last=last->next;
                        q->next=A;
                        p->next=tmp;
                        break;
                    }
                }
                if(A==q) p=p->next;
            }
            return vn.next;
        }
    };

Log in to reply
 

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