用插入排序对链表排序
样例
Given 1->3->2->0->null, return 0->1->2->3->null
法1:刚开始没看到是插入排序 用的冒泡法
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: The head of linked list.
*/
ListNode *insertionSortList(ListNode *head) {
// write your code here
for(ListNode *p1=head;p1!=NULL;p1=p1->next)
{
for(ListNode *p2=head;p2!=NULL;p2=p2->next)
{
int temp;
if(p1->val < p2->val)
{
temp=p1->val;
p1->val=p2->val;
p2->val=temp;
}
}
}
return head;
}
};
法2:插入排序(我不会插入法 回头看了在写吧。。)