leetcode---insertion-sort-list---链表
2018-01-21 21:31
459 查看
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 *findPre(ListNode *head, int val) { ListNode *pre; ListNode *cur = head; while(cur && cur->val <= val) { pre = cur; cur = cur->next; } return pre; } ListNode *insertionSortList(ListNode *head) { if(!head || !head->next) return head; ListNode *newH = new ListNode(INT_MIN); while(head) { ListNode *next = head->next; ListNode *pre = findPre(newH, head->val); head->next = pre->next; pre->next = head; head = next; } return newH->next; } };
相关文章推荐
- [LeetCode] Insertion Sort List 单向链表插入排序
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- LeetCode 147 Insertion Sort List(链表)
- 【leetcode 链表 】Insertion Sort List
- LeetCode | Insertion Sort List(插入法排序链表)
- leetcode——Insertion Sort List 对链表进行插入排序(AC)
- [LeetCode] Insertion Sort List 链表插入排序
- LeetCode Insertion Sort List 链表插入排序
- [LeetCode147]Insertion Sort List(链表插入排序)
- Leetcode Insertion Sort List 插入排序链表
- leetcode_c++:链表:Insertion Sort List(147)
- LeetCode:Insertion Sort List//链表插入排序
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- LeetCode 147 Insertion Sort List(链表插入排序)
- LeetCode Insertion Sort List 链表的插入排序
- [Leetcode] insertion sort list 链表插入排序
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- LeetCode-Insertion Sort List-链表插入排序-链表操作
- 【leetcode】【单链表】【147】Insertion Sort List
- leetcode Insertion Sort List(链表插入排序)