题目:
题解:
cpp
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;
}