Hey there, fellow coders! Today, we're going to dive into a classic problem that often pops up in technical interviews and real-world programming scenarios: merging two sorted linked lists. Don't worry if it sounds intimidating at first – by the end of this post, you'll be merging lists like a pro!
Imagine you have two linked lists, both already sorted in ascending order. Your task is to merge them into a single sorted linked list. Sounds simple enough, right? Well, the trick is to do it efficiently and elegantly.
For example, let's say we have two lists:
List 1: 1 -> 3 -> 5 -> 7 List 2: 2 -> 4 -> 6 -> 8
Our goal is to merge them into:
Merged List: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
The beauty of this problem is that we can leverage the fact that both input lists are already sorted. This allows us to use a straightforward, single-pass approach. Here's the game plan:
Let's implement this solution in Python. First, we'll define our ListNode
class:
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next
Now, here's the function to merge two sorted linked lists:
def mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode: # Create a dummy node to start our merged list dummy = ListNode(0) current = dummy # Traverse both lists simultaneously while l1 and l2: if l1.val <= l2.val: current.next = l1 l1 = l1.next else: current.next = l2 l2 = l2.next current = current.next # Append any remaining nodes if l1: current.next = l1 if l2: current.next = l2 # Return the merged list (excluding the dummy node) return dummy.next
Let's walk through this code step by step:
We start by creating a dummy node. This is a common trick in linked list problems to avoid having to handle the head of the list as a special case.
We use a current
pointer to keep track of where we are in the merged list as we build it.
The while
loop continues as long as both input lists have nodes left to process.
Inside the loop, we compare the values of the current nodes in both lists. The smaller value gets added to our merged list, and we move the pointer for that list forward.
After the loop, one of the lists might still have nodes left. We append these remaining nodes to our merged list.
Finally, we return dummy.next
, which is the actual head of our merged list (remember, dummy
was just a placeholder to make our life easier).
Imagine you're building a music streaming app. You have two playlists, both sorted by song release date. You want to merge these playlists while maintaining the chronological order. This algorithm would be perfect for that!
If you encounter this problem in an interview, here are some pro tips:
23/09/2024 | DSA
16/11/2024 | DSA
13/10/2024 | DSA
06/12/2024 | DSA
15/11/2024 | DSA
08/12/2024 | DSA
16/11/2024 | DSA
23/09/2024 | DSA
06/12/2024 | DSA
23/09/2024 | DSA
06/12/2024 | DSA
03/09/2024 | DSA