list - insertion sort - AC
2014-01-21 12:29
344 查看
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL) return head; ListNode *cur = head->next; ListNode *tmp = NULL; while (cur) { tmp = head; while (tmp->val < cur->val && tmp!=cur) { tmp = tmp->next; } // 0 0 ;cur = cur->next; /* tmp->val>=cur->val && tmp == cur; 1.1 tmp->val > cur->val && tmp == cur; rediculous `` 1.2 tmp->val == cur->val && tmp == cur; cur = cur ->next; */ // 1 0 ;tmp->val < cur->val && tmp == cur; rediculous never happen; cur=cur->next; // 0 1 ;insert; the following if is for make the logical covering full if (tmp->val >= cur->val && tmp != cur) { int first = cur->val; while (tmp!=cur) { int second = first; first = tmp->val; tmp->val = second; tmp = tmp->next; } cur->val = first; } cur=cur->next; } return head; } };
be careful of the comment above it can solve the logical full cover problem
相关文章推荐
- LeetCode-Insertion Sort List[AC源码]
- leetcode——Insertion Sort List 对链表进行插入排序(AC)
- Sort a linked list using insertion sort
- Leetcode -- Insertion Sort List
- #173 Insertion Sort List
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- leetcode insertion-sort-list
- LeetCode147: Insertion Sort List
- LeetCode Insertion Sort List
- leetcode insertionSortList
- leetcode做题总结,题目Insertion Sort List 2013/11/12
- Insertion Sort List —— LeetCode
- 链表的插入排序 Insertion Sort List
- Insertion Sort List (LeetCode)
- 【LeetCode】Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- LeetCode -- Insertion Sort List
- [LeetCode] Insertion Sort List
- leetcode[147]Insertion Sort List