Java Program to Merge Two Sorted Linked Lists in New List Last Updated : 30 Nov, 2022 Comments Improve Suggest changes Like Article Like Report We are given two sorted List and our goal is to merge these two lists into a new list. For that, we have to write one function which will take two List as an argument which is sorted in increasing order. This function will Merge these two List into one List in increasing order. Input List 1 : 1-> 3-> 4-> 9->10 List 2 : 2-> 5-> 6-> 9 Output New List : 1-> 2-> 3-> 4-> 5-> 6-> 9-> 9-> 10 We have two approaches to solve this problem: IterativeRecursive Method 1: Iterative Approach The idea behind this approach is we will take one extra node in the new list which is the Head node of the list.We will take one variable of the type list which is always at the last node of the list so that the appending of a new node becomes easier.We will iterate the loop and check for the smaller element from both lists and append that node to the resultant list.If we reached the end of any list then we will simply append the remaining nodes from the second list. Implementation: Java // Java Program to Merge Two Sorted // Linked Lists in New List // Iteratively import java.io.*; public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } class GFG { public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { // New List ListNode result = new ListNode(-1); // variable to point the last node of the list. ListNode p = result; // Iterate the loop while (l1 != null && l2 != null) { // Find the smaller element and append it to the // list. if (l1.val <= l2.val) { p.next = l1; l1 = l1.next; } else { p.next = l2; l2 = l2.next; } // Update the variable p = p.next; } // If anyone list become empty append the remaining // list element of other list. if (l1 == null) { p.next = l2; } else if (l2 == null) { p.next = l1; } // Return the resultant list without first extra // node return result.next; } // A utility function to print linked list static void printList(ListNode node) { while (node != null) { System.out.print(node.val + " "); node = node.next; } } // Driver code public static void main(String[] args) { ListNode head1 = new ListNode(1); head1.next = new ListNode(3); head1.next.next = new ListNode(5); // 1->3->5 LinkedList created ListNode head2 = new ListNode(0); head2.next = new ListNode(2); head2.next.next = new ListNode(4); // 0->2->4 LinkedList created ListNode mergedhead = mergeTwoLists(head1, head2); printList(mergedhead); } } Output0 1 2 3 4 5 Time Complexity: O(N) Auxiliary Space: O(1) Method 2: Recursive Approach One can solve this problem by using the recursion approach. The function will take two sorted lists as an argument.If any list is empty then it simply returns the remaining elements from the other list.Otherwise, it will check for the smaller element from both lists, append the smaller node to the resultant list and recursively call the function for the next node of the list and another list. Implementation: Java // Java Program to Merge Two Sorted // Linked Lists in New List // Recursively import java.io.*; public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } class GFG { public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { // New List ListNode result = null; // If anyone list is empty then returns the // remaining elements of other list if (l1 == null) { return l2; } else if (l2 == null) { return l1; } // Find the smaller element and recursively call the // function with next node if (l1.val <= l2.val) { result = l1; result.next = mergeTwoLists(l1.next, l2); } else { result = l2; result.next = mergeTwoLists(l1, l2.next); } // Return the resultant list return (result); } // A utility function to print linked list static void printList(ListNode node) { while (node != null) { System.out.print(node.val + " "); node = node.next; } } // Driver code public static void main(String[] args) { ListNode head1 = new ListNode(23); head1.next = new ListNode(35); head1.next.next = new ListNode(65); // 23->35->65 LinkedList created ListNode head2 = new ListNode(43); head2.next = new ListNode(59); head2.next.next = new ListNode(60); // 43->59->60 LinkedList created ListNode mergedhead = mergeTwoLists(head1, head2); printList(mergedhead); } } Output23 35 43 59 60 65 Time Complexity: O(N) Auxiliary Space: O(N) for call stack since using recursion Comment More infoAdvertise with us Next Article Java Program to Merge Two Sorted Linked Lists in New List M meetsuvariya Follow Improve Article Tags : Linked List Java Java Programs DSA Practice Tags : JavaLinked List Similar Reads Java Program To Merge Two Sorted Lists (In-Place) Given two sorted lists, merge them so as to produce a combined sorted list (without using extra space).Examples: Input: head1: 5->7->9 head2: 4->6->8 Output: 4->5->6->7->8->9 Explanation: The output list is in sorted order. Input: head1: 1->3->5->7 head2: 2->4 Output: 1->2->3->4->5->7 Explanation: T 5 min read Java Program To Merge K Sorted Linked Lists - Set 1 Given K sorted linked lists of size N each, merge them and print the sorted output. Examples:Â Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11->NULL Output: 0->1->2->3->4->5->6->7->8->9->10->11 Merged lists in a sorted order where every element is greater t 6 min read Java Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5->10->15->40 b: 2->3->20 Output: res: 40->20->15->10->5->3->2 Input: a: NULL b: 2->3->20 Output: res: 20- 4 min read Java Program To Merge K Sorted Linked Lists Using Min Heap - Set 2 Given k linked lists each of size n and each list is sorted in non-decreasing order, merge them into a single sorted (non-decreasing order) linked list and print the sorted linked list as output.Examples: Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL l 5 min read Java Program To Subtract Two Numbers Represented As Linked Lists Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller from the larger ones.It may be assumed that there are no 5 min read Java Program To Add Two Numbers Represented By Linked Lists- Set 1 Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input:Â List1: 5->6->3 // represents number 563Â List2: 8->4->2 // represents number 842Â Output:Â Resultant list: 1->4->0->5 // re 4 min read Java Program For Adding Two Numbers Represented By Linked Lists- Set 2 Given two numbers represented by two linked lists, write a function that returns the sum list. The sum list is linked list representation of the addition of two input numbers. It is not allowed to modify the lists. Also, not allowed to use explicit extra space (Hint: Use Recursion). Example : Input: 8 min read Java Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l 3 min read Java Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2- 5 min read How to Merge Two LinkedHashSet Objects in Java? Use the addAll() method to merge two LinkedHashSet objects or append elements of one LinkedHashSet object to another LinkedHashSet object. Comparing with the set, addAll() method is used as Union. The addAll method adds all the elements of the specified collection to this set object. Example: Input 1 min read Like