0147.cpp (634B)
1 class Solution { 2 public: 3 ListNode *insertionSortList(ListNode *head) const { 4 ListNode dummy(-1, head); 5 ListNode *prev = &dummy, *crnt = head; 6 7 for (; crnt; prev = crnt, crnt = crnt->next) { 8 if (prev->val <= crnt->val) continue; 9 for (ListNode *p = &dummy, *c = dummy.next; p != prev; p = c, c = c->next) { 10 if (c->val <= crnt->val) continue; 11 prev->next = crnt->next; 12 crnt->next = p->next; 13 p->next = crnt; 14 crnt = prev; 15 break; 16 } 17 } 18 19 return dummy.next; 20 } 21 };