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;
}
};
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;
}
};
相关文章推荐
- leetcode Insertion Sort List(链表插入排序)
- LeetCode Insertion Sort List(单链表插入排序)
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- LeetCode:Insertion Sort List//链表插入排序
- Leetcode Insertion Sort List 插入排序链表
- [LeetCode147]Insertion Sort List(链表插入排序)
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- LeetCode Insertion Sort List 链表的插入排序
- LeetCode 147 Insertion Sort List(链表插入排序)
- Insertion Sort List 链表的插入排序
- LeetCode-Insertion Sort List[AC源码]
- 【leetcode 链表 】Insertion Sort List
- leetcode_c++:链表:Insertion Sort List(147)
- Leetcode刷题记——147. Insertion Sort List(插入排序链表)
- 【LeetCode】Sort List 解题报告(对链表进行归并排序)
- 链表插入排序(insertion-sort-list)
- Insertion Sort List——链表的插入排序
- [LeetCode] Insertion Sort List 单向链表插入排序
- LeetCode | Insertion Sort List(插入法排序链表)