leetcode 148. Sort List
2017-02-25 10:14
225 查看
class Solution { public: ListNode* sortList(ListNode* head) { if (head == nullptr || head->next == nullptr) { return head; } auto fast = head->next->next, slow = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } auto head2 = sortList(slow->next); slow->next = nullptr; return merge(sortList(head), head2); } private: ListNode* merge(ListNode* h1, ListNode* h2) { auto DummyHead = new ListNode(0); auto head = DummyHead; while (h1 && h2) { if (h1->val < h2->val) { head->next = h1; h1 = h1->next; } else { head->next = h2; h2 = h2->next; } head = head->next; } head->next = (h1 == nullptr) ? h2 : h1; return DummyHead->next; } };
相关文章推荐
- [LeetCode] 148. Sort List 解题思路
- LeetCode 148. Sort List
- <LeetCode OJ> 148. Sort List
- 【LeetCode】148. Sort List
- LeetCode 148. Sort List 单链表排序 Python Solution
- [LeetCode] 148. Sort List
- Leetcode 148. Sort List
- [Leetcode] 148. Sort List 解题报告
- leetcode 148. Sort List
- leetcode 148. Sort List
- LeetCode 148. Sort List(java)
- [leetcode] 【排序】 148. Sort List
- LeetCode 148. Sort List(链表排序)
- [leetcode] 148.Sort List
- LeetCode 148. Sort List(C++版)
- [leetcode]148. Sort List -- JavaScript代码
- [LeetCode] 148. Sort List
- leetcode 148. Sort List 链表归并排序
- 【C++】【LeetCode】148. Sort List
- Leetcode-148. Sort List