c++ easy-understanding


  • 0
    X
    class Solution {
    public:
        ListNode* insertionSortList(ListNode* head) {
            if(head == NULL || head->next == NULL){
                return head;
            }
            ListNode newHead(-1);
            newHead.next = head;
            for(ListNode* j = head; j->next != NULL; ){
                ListNode* i = &newHead;
                for( ; i != j; i = i->next){
                    if(i->next->val > j->next->val){
                        break;
                    }
                }
                if(i != j){
                    ListNode* tmpj = j->next->next;
                    ListNode* tmp = i->next;
                    i->next = j->next;
                    j->next->next = tmp;
                    j->next = tmpj;
                }else{
                    j = j->next;
                }
            }
            return newHead.next;
        }
    };
    

Log in to reply
 

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