Why the merge sort?
If you're not a CS graduate like me and have been grinding endless LeetCode problems and studying DSA, you've probably found yourself wondering: Is there any real meaning to all this?
That moment hit me when I was staring at the merge sort algorithm. I had a very valid question: Why use merge sort when its close cousin quicksort offers the same comforting time complexity of O(n log n)?
That was until I met a different kind of beast: the Linked List.
I like to describe a linked list as a prerequisite to trees—kind of like a one-child tree.
And here's where merge sort truly shines: when you're dealing with linked lists instead of arrays.
Merge sort’s divide-and-conquer philosophy involves splitting the structure (be it a list or linked list) into halves until there’s only one element left, and then merging them back together by comparing elements—essentially deciding “who's the parent of whom.”
Here’s a fun example of where merge sort really makes sense with linked lists.
To give a bit of context—when dividing a linked list, you need to find its middle point. Unlike arrays, you can't just index your way to the middle. So, we use a neat little trick called the fast and slow pointer technique. The idea is simple: the fast pointer moves two steps at a time while the slow pointer moves one. By the time the fast pointer reaches the end, the slow one is sitting right in the middle.
You'll actually see this fast-slow pointer trick used in many other algorithms too—especially for detecting cycles. But in this case, it's all about finding the midpoint to split the list for merge sort.
Below is the code for the leet code problem attached. https://leetcode.com/problems/sort-list/
I hope this helps.
Comments
Post a Comment