Sort a linked list in O(n log n) time using constant space complexity. (Medium)html
分析:數組
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* findMiddle(ListNode* head){ 12 ListNode* chaser = head; 13 ListNode* runner = head->next; 14 while(runner != NULL && runner->next != NULL){ 15 chaser = chaser->next; 16 runner = runner->next->next; 17 } 18 return chaser; 19 } 20 21 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 22 if(l1 == NULL){ 23 return l2; 24 } 25 if(l2 == NULL){ 26 return l1; 27 } 28 ListNode* dummy = new ListNode(0); 29 ListNode* head = dummy; 30 while(l1 != NULL && l2 != NULL){ 31 if(l1->val > l2->val){ 32 head->next = l2; 33 l2 = l2->next; 34 } 35 else{ 36 head->next = l1; 37 l1 = l1->next; 38 } 39 head = head->next; 40 } 41 if(l1 == NULL){ 42 head ->next = l2; 43 } 44 if(l2 == NULL){ 45 head->next = l1; 46 } 47 return dummy->next; 48 } 49 50 ListNode* sortList(ListNode* head) { 51 if(head == NULL || head ->next == NULL){ 52 return head; 53 } 54 ListNode* middle = findMiddle(head); 55 ListNode* right = sortList(middle->next); 56 middle -> next = NULL; 57 ListNode* left = sortList(head); 58 return mergeTwoLists(left, right); 59 } 60 };