Leetcode:Insertion Sort List
2015-02-11 20:43
375 查看
Sort a linked list using insertion sort.
插入排序:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。
实现代码:
插入排序:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。
实现代码:
class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head==NULL || head->next==NULL) return head; ListNode *cur=head; ListNode *helper=new ListNode(0); ListNode *pre; while(cur) { ListNode *next=cur->next; pre=helper; while(pre->next!=NULL && pre->next->val<cur->val) { pre=pre->next; } cur->next=pre->next; pre->next=cur; cur=next; } return helper->next; } };
相关文章推荐
- [Leetcode] #147 Insertion Sort List
- Insertion Sort List(leetcode)
- Leetcode: insertion-sort-list
- LeetCode(147) Insertion Sort List
- leetcode第一刷_Insertion Sort List
- [leetcode] Insertion Sort List
- LeetCode-Insertion Sort List-链表插入排序-链表操作
- Leetcode_Insertion Sort List
- [leetcode]Insertion Sort List @ Python
- leetcode_147题——Insertion Sort List(线性表,插入排序)
- 【leetcode】Insertion Sort List
- Java for LeetCode 147 Insertion Sort List
- LeetCode Insertion Sort List
- LeetCode Insertion Sort List 最新题解
- LeetCode-Insertion Sort List[AC源码]
- Insertion Sort List Leetcode java
- leetcode - Insertion Sort List
- leetcode insertionSortList
- LeetCode: Insertion Sort List
- leetcode. Insertion Sort List