Insertion Sort List
2014-04-15 22:56
176 查看
Sort a linked list using insertion sort.
/** * 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) { ListNode dummy(INT_MIN); dummy.next = head; ListNode* cur = &dummy; while(cur->next) { if(cur->next->val >= cur->val) cur = cur->next; else insert(&dummy, cur, cur->next); } return dummy.next; } void insert(ListNode* head, ListNode* tail, ListNode* node) { ListNode* cur = head; while(cur->next->val <= node->val && cur != tail) { cur = cur->next; } tail->next = node->next; node->next = cur->next; cur->next = node; } };
相关文章推荐
- leetcode. Insertion Sort List
- insertion-sort-list——链表、插入排序、链表插入
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- [leetcode]Insertion Sort List
- 链表插入排序(insertion-sort-list)
- Insertion Sort List
- LeetCode: Insertion Sort List
- Sort a linked list using insertion sort
- Insertion Sort List
- [LeetCode#147]Insertion Sort List
- LeetCode---Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- LeetCode:Insertion Sort List
- Foundation Sorting: Single List Insertion Sort
- Insertion Sort List
- LeetCode 147 Insertion Sort List
- Insertion Sort List