Problem Summary
Sort a linked list in O(NlogN) time using constant space complexity.
Solution
I tried merge sort and quick sort. The merge sort worked perfectly, but the quick sort got TLE(time limit exceeded) for some cases.
When the given list is already ascending or almost in order, the time complexity of quick sort would grow to O(N^2). I haven’t found a way to optimize it yet. If you have any suggestions or would like to discuss with me, please leave a comment or email me, thanks in advance!
Code 1 - Merge Sort 1
|
|
Code 2 - Merge Sort 2
|
|
Code 3 - Quick Sort
|
|