[leetcode]Insertion Sort List
2014-02-12 21:06
393 查看
如果那天前先做了这道题就好了。。言归正传,需要两个dummy节点,本质上就是从原来的链表中删除最小的,加到新的链表中去。
class Solution { public: ListNode *insertionSortList(ListNode *head) { ListNode *o_dummy = new ListNode(0); ListNode *n_dummy = new ListNode(0); o_dummy->next = head; ListNode *last = n_dummy; while (o_dummy->next != NULL) { ListNode *prev = o_dummy; ListNode *node = prev->next; // find node with min val ListNode *min_node = node; ListNode *prev_node = prev; while (node != NULL) { if (node->val < min_node->val) { min_node = node; prev_node = prev; } node = node->next; prev = prev->next; } prev_node->next = min_node->next; // insert to new list min_node->next = NULL; last->next = min_node; last = min_node; } return n_dummy->next; } };
相关文章推荐
- LeetCode147: Insertion Sort List
- [Leetcode]#147 Insertion Sort List
- [LeetCode] Insertion Sort List 单向链表插入排序
- Insertion Sort List(leetcode)
- LeetCode-Insertion Sort List-链表插入排序-链表操作
- LeetCode 147 — Insertion Sort List(C++ Java Python)
- leetcode InsertionSort list
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- Leetcode Insertion Sort List
- leetcode之Insertion Sort List
- LeetCode-Insertion Sort List[AC源码]
- [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——Insertion Sort List 对链表进行插入排序(AC)
- Leetcode:Insertion Sort List
- [LeetCode]Insertion Sort List