[LeetCode]Insertion Sort List
2014-09-24 15:26
316 查看
题目描述:
Sort a linked list using insertion sort.
解决方案:
该题目就是用插入排序来对链表进行排序,很简单,直接上代码。
Sort a linked list using insertion sort.
解决方案:
该题目就是用插入排序来对链表进行排序,很简单,直接上代码。
class Solution { public: ListNode *insertionSortList(ListNode *head) { ListNode *q = head; ListNode *temp = new ListNode(0); temp->val = INT_MIN; temp->next = NULL; ListNode *p = temp; ListNode * pre = NULL; ListNode *k = NULL; ListNode *f = NULL; while (q != NULL) { f = q; q = q->next; k = temp; while(1) { if (k == NULL) { f->next = NULL; pre->next = f; break; } if (f->val < k->val) { f->next = pre->next; pre->next = f; pre = k; break; } pre = k; k = k->next; } pre = NULL; } p = temp->next; temp->next = NULL; delete temp; return p; } };
相关文章推荐
- leetcode insertionSortList
- 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
- leetcode之Insertion Sort List
- leetcode_147题——Insertion Sort List(线性表,插入排序)
- 【leetcode】Insertion Sort List
- Java for LeetCode 147 Insertion Sort List
- LeetCode-Insertion Sort List[AC源码]
- Insertion Sort List Leetcode java
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- Leetcode习题:Insertion Sort List
- [Leetcode]Insertion Sort List
- Insertion Sort List - Leetcode
- Leetcode Insertion Sort List 解题报告