LeetCode – Refresh – Insertion Sort List
2015-03-20 04:07
369 查看
Do not forget to break the inner loop, when you find the insert position.
/** * 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 || !head->next) return head; ListNode *result = new ListNode(0); ListNode *old = new ListNode(0); old->next = head; while (old->next) { ListNode *tmp = old->next, *runner = result; old->next = tmp->next; while (runner) { if (!runner->next || tmp->val < runner->next->val) { tmp->next = runner->next; runner->next = tmp; break; } runner = runner->next; } } return result->next; } };
相关文章推荐
- LeetCode - Insertion Sort List
- LeetCode - Insertion Sort List
- [Leetcode] Insertion Sort List (Java)
- leetcode-Insertion Sort List
- LeetCode—Insertion Sort List 解题报告
- leetcode - Insertion Sort List
- LeetCode Insertion Sort List 最新题解
- 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]Insertion Sort List
- LeetCode题解:Insertion Sort List
- LeetCode | Insertion Sort List
- Leetcode_Insertion Sort List
- 【LeetCode with Python】 Insertion Sort List
- Leetcode: Insertion Sort List