【LeetCode】Insertion Sort List
2014-08-11 00:19
337 查看
Sort a linked list using insertion sort.
我自己AC的代码花费的时间比较长,下面是看了博客http://www.cnblogs.com/TenosDoIt/p/3422296.html
之后重新写的
我自己AC的代码花费的时间比较长,下面是看了博客http://www.cnblogs.com/TenosDoIt/p/3422296.html
之后重新写的
class Solution { public: ListNode *insertionSortList(ListNode *head) { if (head == NULL || head->next == NULL) { return head; } ListNode *sorted_head = head; ListNode *sorted_rear = head; ListNode *unsorted = head->next; while (unsorted) { ListNode *cur = unsorted; unsorted = unsorted->next; if (cur->val <= sorted_head->val) { cur->next = sorted_head; sorted_head = cur; } else if (sorted_rear->val <= cur->val) { sorted_rear->next = cur; sorted_rear = cur; } else { ListNode *less = sorted_head; while (less->next && less->next->val < cur->val) { less = less->next; } cur->next = less->next; less->next = cur; } } sorted_rear->next = NULL; return sorted_head; } };
相关文章推荐
- [LeetCode刷题] Insertion Sort List
- leetcode---insertion-sort-list---链表
- Leetcode155: Insertion Sort List
- [LeetCode] Insertion Sort List
- 【leetcode】Insertion Sort List
- Leetcode:Insertion Sort List
- leetcode 【 Insertion Sort List 】 python 实现
- Insertion Sort List | leetcode
- LeetCode——Insertion Sort List
- leetcode -- Insertion Sort List
- leetcode-004 insertion sort list
- [LeetCode]Insertion Sort List
- LeetCode Insertion Sort List(单链表插入排序)
- LeetCode—Insertion Sort List 解题报告
- leetcode之Insertion Sort List
- leetcode-Insertion Sort List
- leetcode Insertion Sort List
- LeetCode Insertion Sort List
- Leetcode Insertion Sort List 插入排序链表
- LeetCode Insertion Sort List