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. 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. If you find the blog post interesting, please share it so that more people know about it. And look forward to more of my blog posts on the topic of Data Structure & Algorithm on TipsMake's tech blog!
You should read it
- Linked list data structure (Linked List)
- Data Link List structure (Circular Linked List)
- Shift linked list problem
- Microsoft adds a page to manage devices sharing the same user account on Windows 11
- The To-do List is good, the Done List is very good, but the Do-Not-Do List is much better
- How to create a drop list in Excel 2016
- Work with lists in PowerPoint 2016
- How to Simplify Your To-Do List
May be interested
- How to split, merge first name in Excelhow to split, merge first name in excel. normally you enter the list of staff, students, students .... the first name and last name fields are on the same column. however, in the process of working with some software, it is necessary to get the first and last name of the field ..
- How to merge PDF files, merge and join multiple PDF files into a single fileto be able to merge pdf files, combine multiple pdf files into a single file, we can choose support software to install on the computer, or use online applications.
- How to merge multiple PDF files into one PDF file in Mac OS Xto be able to merge pdf files into a single file, we will use other specialized software. however, if on mac os x, everything is much simpler.
- Fix errors can not merge hard drives, partitions on Windowsalthough the process of merging hard drives and partitions on windows 10 is extremely simple, you have done many times without success, causing you to question why is it so.
- Merge keyboard shortcuts in Excelmerge keyboard shortcuts in excel. anyone who uses excel knows the merge tool to mix adjacent cells together into one cell. however, not all users are proficient in using the merger shortcut to save time. today dexterity software will introduce you to read some keyboard shortcuts to merge in excel.
- How to merge a hard drive in Windows 10 does not lose data with MiniTool Partition Wizardsome tools that you apply in the past to merge hard drives and increase data have some unexpected flaws that make you uncomfortable and insecure. to remedy this situation, tipsmake will present you with a new tool, easy and effective implementation.
- How to fix number formatting errors when using Mail Merge in Wordmicrosoft word's mail merge feature is one of the features that users love. it works when creating custom labels, templates, emails or reports.
- Instructions for deleting duplicate contacts on Androidsometimes you restore contacts from a variety of sources thanks to the synchronization feature. however, it does arise a problem that after synchronizing, your contacts appear many duplicate contacts. so the following article will show you how to delete duplicate contacts on android device. stay tuned if you encounter the same problem.
- Guide to playing Merge Tactics Kingdom Defense for newbiesmerge tactics kingdom defense is one of the most popular mobile defense strategy games today. in merge tactics, the player's task is to do whatever it takes to protect your kingdom from the invading forces.
- How to use mail merge in Word to merge textin word, there is a mail merge feature that merges letters, emails, contracts, salary slips, and notices to compose bulk content with similar content without you having to enter it manually.