Solve LeetCode problem Merge Two Sorted Lists
In this blog, we will solve the LeetCode problem Merge Two Sorted Lists.
Problem Overview
The problem is to merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. In simple sense, we have to merge two sorted linked lists into a single sorted linked list. For example, if the input lists are:
1list1 = 1 -> 2 -> 42list2 = 1 -> 3 -> 434sortedList = 1 -> 1 -> 2 -> 3 -> 4 -> 4
Video Explanation
You can find the video explanation of this problem on my YouTube channel:
Approach
We can solve this problem using a simple iterative approach. We will create a new linked list and keep adding the smaller node from the two input lists to the new list. We will keep track of the tail of the new list and keep adding the smaller node to the tail. Once we reach the end of one of the input lists, we will add the remaining nodes of the other list to the new list.
The algorithm can be summarized as follows:
Create a new linked list to store the merged list.
- Initialize the new linked list with a dummy node in order to avoid handling the edge case of an empty list.
- Initialize a tail pointer to the dummy node.
- Initialize two pointers,
first
andsecond
, to the heads of the input lists.
Iterate through the input lists until one of the pointers reaches the end.
Compare the values of the
first
andsecond
pointers.- Store the smaller node's next pointer in a temporary variable.
- Set the smaller node's next pointer to
null
. - Set the tail's next pointer to the smaller node.
- Move the tail pointer to the smaller node.
Move the tail pointer to its next node.
If first is null and second is not null, set the tail's next pointer to the second pointer.
If second is null and first is not null, set the tail's next pointer to the first pointer.
Return the next node of the dummy node.
Solution
1var mergeTwoLists = function (list1, list2) {2 let newList = new ListNode(false) // dummy node34 let tail = newList5 let first = list16 let second = list278 while (first && second) {9 // compare the values of the first and second pointers10 if (first.val < second.val) {11 let temp = first.next12 first.next = null // Remove the link to the next node13 tail.next = first // Add the smaller node to the new list14 first = temp15 } else {16 let temp = second.next17 second.next = null // Remove the link to the next node18 tail.next = second // Add the smaller node to the new list19 second = temp20 }2122 tail = tail.next // Update the tail pointer23 }2425 // Add the remaining nodes of the input lists to the new list26 if (!first && second) {27 tail.next = second28 }29 if (first && !second) {30 tail.next = first31 }3233 return newList.next34}
Shameless Plug
I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram : @thatanjan
- Twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: