Sort a linked list in ascending order using merge sort with O(n log n) time complexity
Merge sort divides the list in half recursively until single nodes remain, then merges sorted halves bottom-up. Each level of recursion processes all n elements exactly once (during merging), and there are log n levels. The slow/fast pointer technique finds the midpoint in O(n) without random access.
Unlike quicksort (O(n²) worst case), merge sort guarantees O(n log n) for all inputs—making it the preferred choice for linked lists.
Array merge sort requires O(n) auxiliary space for merging. Linked list merge sort achieves O(1) merge space because we can rearrange pointers without copying elements. The only space cost is the recursion stack, which has depth log n due to balanced splitting.
Bottom-up iterative merge sort eliminates the recursion stack entirely, achieving O(1) auxiliary space for linked lists.