[LeetCode]Insertion Sort List
2013-11-20 14:01
441 查看
/** * 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) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(head == NULL) return head; ListNode dummy(-1); dummy.next = head; ListNode* prev = head; ListNode* cur = prev->next; while(cur) { if(cur->val >= prev->val)//greater or equal { prev = cur; cur = cur->next; } else//smaller { //first find the insert position ListNode* insertPrev = &dummy; ListNode* insertCur = dummy.next; while(insertCur->val < cur->val) { insertPrev = insertCur; insertCur = insertCur->next; } //then insert prev->next = cur->next; cur->next = insertCur; insertPrev->next = cur; cur = prev->next; } } return dummy.next; } };
相关文章推荐
- Leetcode: Insertion Sort List
- leetcode之Insertion Sort List
- LeetCode-Insertion Sort List
- LeetCode_Insertion Sort List
- leetcode_Sort a linked list using insertion sort.
- [LeetCode] Insertion Sort List
- LeetCode Insertion Sort List
- Insertion Sort List Leetcode
- LeetCode: Insertion Sort List(外加对单链表类题目的思考总结)
- LeetCode Insertion Sort List
- 【leetcode】 Insertion_Sort_List
- [LeetCode]Insertion Sort List
- leetCode #147 Insertion Sort List
- [LeetCode]题解(python):147-Insertion Sort List
- Insertion Sort List -- leetcode
- Leetcode Insertion Sort List 解题报告
- [LeetCode]Insertion Sort List
- LeetCode 5 Insertion Sort List
- The Solution to LeetCode 147 Insertion Sort List
- LeetCode | Insertion Sort List