Table of Contents
Merge Linked List Problem is easier to approach with a clear overview and reliable steps. This guide organizes the essential information, highlights practical details, and explains what to check along the way.
Key Takeaways
- Review introduce.
- Explore topic.
- Understand solution 1: use list as intermediary.
Introduce
Continuing the Linked List topic in the Algorithm & Data Structure series, today I will introduce to you the Merge Linked List problem. In this article, I will give 2 ways to solve the problem and analyze the space and time complexity. I will assume that you already know about the linked list data structure when reading this article. If not, you can read the document "Basic linked list" of Stanford University, USA here.
Topic
Write a function that takes in 2 heads of two sorted Singly linked lists. Write a function that merges the two linked lists and returns the head of the merged sorted linked list.
Input:
Output:
In the next section, I will give two ways to solve this problem.
Solution 1: Use List as Intermediary
Similar to the previous linked list problems, we can solve this problem through lists and then reconstruct a linked list. Here is my description:

The detailed steps are as follows:
- First we convert two linked lists into two lists.
- Then we merge those two lists together.
- Newly created sort list
- Construct new linked list based on sorted list.
My solution to this approach:
Space and Time Complexity Analysis:
Note: m and n are the lengths of the two linked lists respectively.
Space complexity is O(n + m)
The space complexity to listify two linked lists is O(n + m). The space complexity to construct a linked list from the new list is O(n + m). Therefore, the total space complexity is O(n + m).
Time complexity is O((n+m)log(n+m)).
Time complexity to listify two linked lists is O(n+m) Time complexity to sort the list is O((n + m)log(n + m)) Time complexity to construct the linked list from the new list is O(n+m) Therefore, total time complexity is O((n + m)log(n + m).
Solution 2: Merge in Place
Instead of going through the list like the above method, this way we will merge two linked lists right on the selected one. Here is my description:

The detailed steps are as follows:
- Choose the head with the smaller value as the center - where the remaining linked list nodes will be inserted.
- Use two pointers if to insert nodes into the central linked list in order.
- Loop through all nodes in both linked lists.
My solution is as follows:
Space and time complexity analysis:
Note: m and n are the lengths of the two linked lists respectively.
Space complexity is O(1)
- Since the entire algorithm is merged in place, the space complexity constant - O(1).
Time complexity is O(n+m)
- The time complexity to iterate through each element of two linked lists is O(n + m).
III. Conclusion
The above two solutions are not all the ways to merge two linked lists, but they are two quite typical ways for this classic problem. Hopefully this blog post can help you in some way. Please share it so that more people know about it if you find the blog post interesting. And look forward to more of my blog posts on the topic of Data Structure & Algorithm on this guide's tech blog!
FAQ
What should I know first about Merge Linked List Problem?
Introduce Continuing the Linked List topic in the Algorithm & Data Structure series, today I will introduce to you the Merge Linked List problem.
How do I get the best results with Merge Linked List Problem?
Use current software or equipment, follow the steps in order, review the recommended settings, and test one change at a time so you can identify what improves the result.
What should I do if Merge Linked List Problem doesn't work as expected?
Check compatibility, permissions, connectivity, and version-specific settings. Restart the relevant device or app, then repeat the process carefully before trying a more advanced fix.
Reader Comments 0
Sign in with email or Google to join the discussion.