leetcode_sort_list 发表于 2016-12-01 难度:Medium 解题思路:可以快排,也可以归并排序。快排中加入了pivot_list,用来存储和pivot一样的值。 快排代码如下: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* sortList(ListNode* head) { if(head == NULL || head->next == NULL) return head; ListNode*pivot_head = head; ListNode*first_head = NULL; ListNode*second_head = NULL; Partition(pivot_head, first_head, second_head); if(first_head != NULL) first_head = sortList(first_head); if(second_head != NULL) second_head = sortList(second_head); ListNode *dummy = new ListNode(-22); ListNode *cur = dummy; if(first_head != NULL) { cur->next = first_head; cur = cur->next; while(cur->next != NULL) { cur = cur->next; } } if(pivot_head != NULL) { cur->next = pivot_head; cur=cur->next; while(cur->next != NULL) { cur = cur->next; } } cur->next = second_head; return dummy->next; } void Partition(ListNode * &pivot_head, ListNode * &first_head, ListNode * &second_head) { // cout<<"pivot:"<<pivot<<endl; if(pivot_head == NULL) return; ListNode *pivot_dummy = new ListNode(-22); ListNode *first_dummy = new ListNode(-1); ListNode *second_dummy = new ListNode(-11); pivot_dummy->next = pivot_head; int pivot = pivot_head->val; ListNode *pivot_cur = pivot_dummy; ListNode *first_cur = first_dummy; ListNode *second_cur = second_dummy; while(pivot_cur->next != NULL) { // cout<<"first_cur->next loop"<<endl; if(pivot_cur->next->val == pivot) { pivot_cur = pivot_cur->next; } else if(pivot_cur->next->val > pivot) { second_cur->next = pivot_cur->next; pivot_cur->next = pivot_cur->next->next; second_cur = second_cur->next; second_cur->next = NULL; } else if(pivot_cur->next->val < pivot) { first_cur->next = pivot_cur->next; pivot_cur->next = pivot_cur->next->next; first_cur = first_cur->next; first_cur->next = NULL; } } first_head = first_dummy->next; second_head = second_dummy->next; pivot_head = pivot_dummy->next; }}; 运行结果:49ms,超过74.88% 归并排序如下: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* sortList(ListNode* head) { if(head == NULL || head->next == NULL) return head; ListNode *dummy = new ListNode(-1); dummy->next = head; ListNode *slow = dummy; ListNode *fast = dummy; while(fast->next != NULL && fast->next->next != NULL) { slow = slow->next; fast = fast->next->next; } ListNode *new_head = slow->next; slow->next = NULL; ListNode *left_ = sortList(head); ListNode *right_ = sortList(new_head); return mergeSortedList(left_, right_); } ListNode* mergeSortedList(ListNode *left_head,ListNode *right_head) { ListNode *dummy =new ListNode(-1); ListNode *cur = dummy; ListNode *left_cur = left_head; ListNode *right_cur = right_head; while(left_cur != NULL && right_cur != NULL) { if(left_cur->val < right_cur->val) { cur->next = left_cur; left_cur = left_cur->next; cur = cur->next; cur->next = NULL; } else { cur->next = right_cur; right_cur = right_cur->next; cur = cur->next; cur->next = NULL; } } if(left_cur != NULL) { cur->next = left_cur; } if(right_cur != NULL) { cur->next = right_cur; } return dummy->next; }}; 运行结果:59ms,超过27.49%