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.
- 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 two sorted linked lists such that merged list is in reverse order
- Sorted merge of two sorted doubly circular linked lists
- Merge a linked list into another linked list at alternate positions
- Merge K sorted linked lists | Set 1
- Merge two sorted linked lists
- Merge k sorted linked lists | Set 2 (Using Min Heap)
- Remove duplicates from an unsorted linked list
- Replace nodes with duplicates in linked list
- Insert value in sorted way in a sorted doubly linked list
- Remove duplicates from an unsorted doubly linked list
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
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.