# Middle-题目63：147. Insertion Sort List

Sort a linked list using insertion sort.

struct ListNode* insertionSortList(struct ListNode* head) {
else {
struct ListNode* sortedHead = (struct ListNode*)malloc(sizeof(struct ListNode));

for (struct ListNode* node = head->next; node; node = node->next) {
if(node->val <= sortedHead->val) { // this node is smaller than the smallest node of sortedHead
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = node->val;
}
else {
struct ListNode* temp;
for(temp = sortedHead; temp; temp = temp->next) {
if(temp->val <= node->val && (!temp->next || temp->next->val >node->val)) { //insert this node after temp
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = node->val;
newNode->next = temp->next;
temp->next = newNode;
break;
}
}
}
}
}
}

76ms,beats 25%,众数72ms,25%
cmershen的碎碎念：

#### LeetCode 147 — Insertion Sort List（C++ Java Python）

2014-02-27 21:03:49

#### 147. Insertion Sort List （python）

2016-09-19 22:11:41

#### 147. Insertion Sort List【M】Java

2016-02-29 15:41:57

#### 【LEETCODE】147- Insertion Sort List [Python]

2016-03-04 20:34:34

#### LeetCode(147) Insertion Sort List

2014-01-28 11:32:14

#### leetcode_效率题解_[python/C++]_147. Insertion Sort List（链表插入排序）

2016-11-22 21:23:15

#### leetcode之147. Insertion Sort List（C++解法）

2016-09-11 20:14:38

#### Leetcode Insertion Sort List 解题报告

2014-02-11 09:23:37

#### Insertion Sort List -- LeetCode

2014-03-14 22:45:24

#### 147. Insertion Sort List。

2017-09-25 21:45:46