1. 题目
Sort a linked list using insertion sort.
使用插入排序对链表排序。
2. 分析
3. 代码
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode dummy(INT_MIN);
ListNode *cur = &dummy;
while(head != NULL)
{
ListNode *t = head->next;
cur = &dummy;
while(cur->next != NULL && cur->next->val <= head->val)
cur = cur->next;
head->next = cur->next;
cur->next = head;
head = t;
}
return dummy.next;
}
};