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.
- Merge K sorted Doubly Linked List in Sorted Order
- Sorted merge of two sorted doubly circular linked lists
- In-place Merge two linked lists without changing links of first list
- Merge two sorted linked lists such that merged list is in reverse order
- Merge two unsorted linked lists to get a sorted list
- Remove duplicates from a sorted linked list
- Remove all occurrences of duplicates from a sorted Linked List
- Remove duplicates from a sorted doubly linked list
- Remove duplicates from a sorted linked list using recursion
- Merge a linked list into another linked list at alternate positions
- Merge two sorted linked lists
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Given a linked list which is sorted, how will you insert in sorted way
- Insert value in sorted way in a sorted doubly linked list
- Merge K sorted linked lists | Set 1
- Merge k sorted linked lists | Set 2 (Using Min Heap)
- Create new linked list from two given linked list with greater element at each node
- Remove duplicates from an unsorted linked list
- Remove duplicates from an unsorted doubly linked list
- Replace nodes with duplicates in linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.