Merge two sorted linked list of size n1 and n2. The duplicates in two linked list should be present only once in the final sorted linked list.
Input : list1: 1->1->4->5->7 list2: 2->4->7->9 Output : 1 2 4 5 7 9
Approach: Following are the steps:
- Merge the two sorted linked list in sorted manner. Refer recursive approach of this post. Let the final obtained list be head.
- Remove duplicates from sorted linked list head.
1 2 4 5 7 9
Time complexity: O(n1 + n2).
Auxiliary Space: O(1).
Exercise: Get the final sorted linked list without duplicates in a single traversal of the two lists.
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.