您的位置:首页 > 其它

leetcode——Insertion Sort List 对链表进行插入排序(AC)

2014-07-14 22:05 507 查看
Sort a linked list using insertion sort.

class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head == NULL || head->next == NULL)
return head;
ListNode *result;
result->val = INT_MIN;
result->next = NULL;
ListNode *cur=head,*pos,*pre;
while(cur!=NULL)
{
pos = result->next;
pre = result;
while(pos != NULL && pos->val <= cur->val)
{
pre = pos;
pos = pos->next;
}
ListNode *temp = cur->next;
pre->next = cur;
cur->next = pos;
cur = temp;
}
return result->next;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  插入排序 链表