My concise C++ solution with only 10 lines!


  • 0
    F
    class Solution {
    public:
        ListNode* insertionSortList(ListNode* head) {
            ListNode dump(0), *prehead, *to_insert;
            while(head)
            {
                to_insert = head;
                head = head->next;
                for(prehead = &dump; prehead->next && prehead->next->val < to_insert->val; prehead = prehead->next);
                to_insert->next = prehead->next;
                prehead->next = to_insert;
            }
            return dump.next;
        }
    };

Log in to reply
 

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