Given two sorted lists, merge them so as to produce a combined sorted list (without using extra space).
Input : head1: 5->7->9 head2: 4->6->8 Output : 4->5->6->7->8->9 Input : head1: 1->3->5->7 head2: 2->4 Output : 1->2->3->4->5->7
We have discussed different solutions in below post.
Merge two sorted linked lists
In this post, a new simpler solutions are discussed. The idea is to one by one compare nodes and form the result list.
Method 1 (Recursive)
0 1 2 3 4 5
Method 2 (Iterative)
0 1 2 3 4 5
This article is contributed by Mandula Vikitha. If you like GeeksforGeeks and would like to contribute, you can also write an article usin contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Sorted merge of two sorted doubly circular linked lists
- Merge K sorted linked lists | Set 1
- Merge two sorted linked lists
- Merge k sorted linked lists | Set 2 (Using Min Heap)
- Merge two sorted linked lists such that merged list is in reverse order
- Merge Sort for Linked Lists
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- Merge two sorted arrays
- Merge k sorted arrays | Set 1
- In-place Merge two linked lists without changing links of first list
- Merge two sorted arrays with O(1) extra space
- Merge two sorted linked list without duplicates
- Intersection of two Sorted Linked Lists
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
Improved By : prerna saini