# Sort List

> Sort a linked list in O(n log n) time using constant space complexity.

```c++
class Solution {
public:
ListNode *sortList(ListNode *head) {
if(head == NULL || head->next == NULL) {
return head;
}

ListNode* fast = head;
ListNode* slow = head;

//快慢指针得到中间点
while(fast->next && fast->next->next) {
fast = fast->next->next;
slow = slow->next;
}

//将链表拆成两半
fast = slow->next;
slow->next = NULL;

//左右两半分别排序
ListNode* p1 = sortList(head);
ListNode* p2 = sortList(fast);

//合并
return merge(p1, p2);
}

ListNode *merge(ListNode* l1, ListNode* l2) {
if(!l1) {
return l2;
} else if (!l2) {
return l1;
} else if (!l1 && !l2) {
return NULL;
}

ListNode dummy(0);
ListNode* p = &dummy;

while(l1 && l2) {
if(l1->val < l2->val) {
p->next = l1;
l1 = l1->next;
} else {
p->next = l2;
l2 = l2->next;
}

p = p->next;
}

if(l1) {
p->next = l1;
} else if(l2){
p->next = l2;
}

return dummy.next;
}
};
```

# Insertion Sort List

> Sort a linked list using insertion sort.

```c++
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head == NULL || head->next == NULL) {
return head;
}

ListNode dummy(0);

ListNode* p = &dummy;

ListNode* cur = head;
while(cur) {
p = &dummy;

while(p->next && p->next->val <= cur->val) {
p = p->next;
}

ListNode* n = p->next;
p->next = cur;

cur = cur->next;
p->next->next = n;
}

return dummy.next;
}
};
```