###### 147. Insertion Sort List

Sort a linked list using insertion sort.

class Solution {
public:
ListNode* q = tail->next;
while (q){
//extract
tail->next = q->next;

//search
if (tmp->val >= q->val){
q->next = tmp;
}
else{
while (tmp != tail && tmp->next->val < q->val){
tmp = tmp->next;
}
q->next = tmp->next;
tmp->next = q;
if (tmp == tail)
tail = q;
}
//go on
q = tail->next;
}
}
};

class Solution {
public:
ListNode *dummy = new ListNode(0);
while (cur){
ListNode *tmp = cur->next;
insert(dummy, cur);
cur = tmp;
}
return dummy->next;
}
private:
void insert(ListNode *dummy, ListNode* cur){
ListNode *pre = dummy;
while (pre->next && pre->next->val < cur->val)
pre = pre->next;
ListNode *tmp = pre->next;
pre->next = cur;
cur->next = tmp;
}
};

#### 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

#### LeetCode147: Insertion Sort List

2015-05-04 19:24:19

#### 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 java

2017-03-09 16:12:06

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

2016-09-11 20:14:38

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

2014-02-11 09:23:37

## 不良信息举报

147. Insertion Sort List