Sort a linked list inO(NLogN) Time using constant space complexity.
Solving this problem makes me very entangled. Can I use quick sorting? I tried it many times and the result always times out, even when using the stl that comes with the system. Why? Afterwards, fast sorting does not make full use of the consistency of the linked list. To exchange values in fast sorting, you must repeat the chain tables. Even when you save the values in the linked list to an array, the method of re-generating the linked list using the array sorted by the quick sorting algorithm also times out! That is to say, the number of traversal times cannot exceed 1.
However, if merge sorting is used, this problem will not occur. Merging and sorting only requires the traversal of the chain table during merge and splitting, and neither of the two situations requires the whole chain table to be traversed.
The chain table has a notable feature, that is, as long as a starting pointer is given, a chain table can be uniquely identified. The following code utilizes this feature.
1 class Solution {2 public: 3 ListNode * sortList (ListNode * head) {4 if (! Head |! Head-> next) 5 return head; 6 return mergesort (head);} 7 ListNode * mergesort (ListNode * head) {8 if (! Head |! Head-> next) 9 return head; 10 ListNode * fast; 11 ListNode * slow; 12 ListNode * mid; 13 fast = slow = head; 14 while (fast & fast-> next) {15 fast = fast-> next; 16 mid = slow; 17 slow = slow-> next; 18} 19 mid-> next = NULL; 20 ListNode * l = mergesort (head); // who is the first in the linked list after sorting? Must it be a head? 21 ListNode * r = mergesort (slow); // at the beginning, no returned values were recorded. head and slow were used directly. Wrong! 22 return merge (l, r); 23 24} 25 26 ListNode * merge (ListNode * l, ListNode * r) {27 ListNode * temp = new ListNode (0 ); 28 ListNode * p = temp; 29 while (l & r) {30 if (l-> val <r-> val) {31 p-> next = l; 32 l = l-> next; 33} 34 else {35 p-> next = r; 36 r = r-> next; 37} 38 p = p-> next; 39} 40 if (! L) 41 p-> next = r; 42 else43 p-> next = l; 44 p = temp-> next; 45 delete temp; 46 return p; 47} 48 };
By the way, the first time I used the code for fast sorting, I suddenly felt very bad!
1 // The idea of using the data structure and algorithm analysis book 2 int size (ListNode * head) {3 int sum = 0; 4 while (head! = NULL) {5 head = head-> next; 6 sum ++; 7} 8 return sum; 9} 10 11 int num (ListNode * head, int pos) {12 int I; 13 for (I = 0; I <pos; I ++) 14 head = head-> next; 15 return head-> val; 16} 17 void assign (ListNode * head, int pos, int num) {18 int I; 19 for (I = 0; I <pos; I ++) 20 head = head-> next; 21 head-> val = num; 22} 23 void swap1 (ListNode * head, int I, int j) {24 int temp = num (head, I); 25 assign (head, I, num (head, j); 26 assign (head, j, tem P); 27} 28 int partition (ListNode * head, int l, int r, int partition) {29 do {30 while (num (head, ++ l) <STRONG); 31 while (r! = 0) & num (head, -- r)> rows); 32 swap1 (head, l, r); 33} while (l <r); 34 swap1 (head, l, r); 35 return l; 36} 37 void qsort1 (ListNode * head, int I, int j) {38 if (j <= I) 39 return; 40 int partition tindex = (I + j)/2; 41 swap1 (head, partition tindex, j); 42 int k = partition (head, i-1, j, num (head, j); 43 swap1 (head, k, j); 44 qsort1 (head, I, K-1); 45 qsort1 (head, k + 1, j ); 46} 47 class Solution {48 public: 49 ListNode * sortList (ListNode * head) {50 qsort1 (head, 0, size (head)-1); 51 return head; 52} 53 };
Sort list (7)