class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(!head)
return head;
ListNode *newHead = new ListNode(-5001, head);
ListNode *left = head, *right = head -> next;
while(right){
if(left -> val <= right -> val){
left = left -> next;
}
else{
ListNode *temp = newHead;
while(temp -> next -> val <= right -> val)
temp = temp -> next;
left -> next = right -> next;
right -> next = temp -> next;
temp -> next = right;
}
right = left -> next;
}
return newHead -> next;
}
};
Accepted 19/19 cases passed (16 ms) Your runtime beats 72.9 % of cpp submissions Your memory usage beats 98.92 % of cpp submissions (9.2 MB)
|