* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *insertionSortList(struct ListNode *head) {
if (head == NULL) {
return head;
}
struct ListNode *dummyHead = malloc(sizeof(struct ListNode));
dummyHead->val = 0;
dummyHead->next = head;
struct ListNode *lastSorted = head;
struct ListNode *curr = head->next;
while (curr != NULL) {
if (lastSorted->val <= curr->val) {
lastSorted = lastSorted->next;
} else {
struct ListNode *prev = dummyHead;
while (prev->next->val <= curr->val) {
prev = prev->next;
}
lastSorted->next = curr->next;
curr->next = prev->next;
prev->next = curr;
}
curr = lastSorted->next;
}
return dummyHead->next;
}
04-21
880
01-18
1903
11-15
2561
11-15
1066
11-15
2599