1/ 链表插入排序问题。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode *tmp=new ListNode(INT_MIN);
//一个额外的节点
ListNode*cur, *t;
while(head)
{
t=head->next;
//tmp指向的是有序的链表,head指向未排序的原链表
cur=tmp;
//获取插入的位置
while(cur->next && cur->next->val <=head->val)
{
cur=cur->next;
}
//head应该插入到cur的后边
head->next=cur->next;
cur->next=head;//key question.
//原未排序链表前进一步。
head=t;
}
return tmp->next;
}
};
2/ 链表归并排序
/**
* Definition for singly-linked list.
* struct