# Insertion Sort List

Sort a linked list using insertion sort.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *dummy=new ListNode(0);
while(p)
{
ListNode *position=getInsertPosition(dummy,p->val);
ListNode *tmp=position->next;
ListNode *tmp2=p->next;
position->next=p;
p->next=tmp;
p=tmp2;
}
return dummy->next;
}

//寻找链表中的插入位置
ListNode* getInsertPosition(ListNode *dummy,int val)
{
ListNode *p=dummy->next;

if(p==NULL)
{
return dummy;
}
ListNode *pre=dummy;
while(p)
{
if(p->val<val)
{
pre=p;
p=p->next;
}
else
break;
}
return pre;
}
};

#### 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（C++ Java Python）

2014-02-27 21:03:49

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

2014-02-11 09:23:37

#### LeetCode(147) Insertion Sort List

2014-01-28 11:32:14

#### Insertion Sort List -- LeetCode

2014-03-14 22:45:24

#### 插入排序Sort a linked list using insertion sort in Java

2015-08-25 10:41:15

#### [C++]LeetCode: 126 Insertion Sort List (插入排序链表)

2015-01-27 11:20:52

#### LeetCode147: Insertion Sort List

2015-05-04 19:24:19

#### Java实例8 - 插入排序 Insertion Sort

2015-02-26 23:57:21