Given two sorted singly linked lists having n and m elements each, merge them using constant space. First n smallest elements in both the lists should become part of first list and rest elements should be part of second list. Sorted order should be maintained. We are not allowed to change pointers of first linked list.
Input: First List: 2->4->7->8->10 Second List: 1->3->12 Output: First List: 1->2->3->4->7 Second List: 8->10->12
We strongly recommend you to minimize your browser and try this yourself first.
The problem becomes very simple if we’re allowed to change pointers of first linked list. If we are allowed to change links, we can simply do something like merge of merge-sort algorithm. We assign first n smallest elements to the first linked list where n is the number of elements in first linked list and the rest to second linked list. We can achieve this in O(m + n) time and O(1) space, but this solution violates the requirement that we can’t change links of first list.
The problem becomes a little tricky as we’re not allowed to change pointers in first linked list. The idea is something similar to this post but as we are given singly linked list, we can’t proceed backwards with the last element of LL2.
The idea is for each element of LL1, we compare it with first element of LL2. If LL1 has a greater element than first element of LL2, then we swap the two elements involved. To keep LL2 sorted, we need to place first element of LL2 at its correct position. We can find mismatch by traversing LL2 once and correcting the pointers.
Below is C++ implementation of this idea.
First List: 1->2->3->4->7->NULL Second List: 8->10->12->NULL
Time Complexity : O(mn)
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sort a linked list of 0s, 1s and 2s by changing links
- Pairwise swap elements of a given linked list by changing links
- Merge two sorted linked lists such that merged list is in reverse order
- Merge two unsorted linked lists to get a sorted list
- Merge two sorted linked list without duplicates
- Merge a linked list into another linked list at alternate positions
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- Sorted merge of two sorted doubly circular linked lists
- Difference of two Linked Lists using Merge sort
- Merge odd and even positioned nodes of two Linked Lists alternately
- Merge two sorted linked lists
- Recursive selection sort for singly linked list | Swapping node links
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Find intersection point of two Linked Lists without finding the length
- Merge first half and reversed second half of the linked list alternatively
- Merge Sort for Linked Lists
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Merge k sorted linked lists | Set 2 (Using Min Heap)