Merge Two Sorted Lists
Merge two sorted linked lists and return the merged sorted list using recursion or iteration.
๐งฉ Problem Statement
Merge two sorted linked lists and return the merged list as a new sorted linked list.
โ๏ธ Function Signature
public ListNode mergeTwoLists(ListNode l1, ListNode l2)
๐ Recursive Approach
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
- Time Complexity: O(n + m), where
n
andm
are the lengths of the two lists. - Space Complexity: O(n + m) due to recursion stack.
๐งฐ Iterative Alternative
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode current = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
current.next = (l1 != null) ? l1 : l2;
return dummy.next;
}
- Time Complexity: O(n + m)
- Space Complexity: O(1) (iterative, no extra space)
๐งช Sample ListNode Definition & Test Cases
public class MergeSortedListsTest {
public static void main(String[] args) {
ListNode l1 = buildList(new int[]{1, 3, 5});
ListNode l2 = buildList(new int[]{2, 4, 6});
ListNode merged = new MergeSortedLists().mergeTwoLists(l1, l2);
printList(merged); // Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6
ListNode l3 = buildList(new int[]{});
ListNode l4 = buildList(new int[]{0});
printList(new MergeSortedLists().mergeTwoLists(l3, l4)); // Output: 0
}
private static ListNode buildList(int[] values) {
ListNode dummy = new ListNode(-1);
ListNode current = dummy;
for (int val : values) {
current.next = new ListNode(val);
current = current.next;
}
return dummy.next;
}
private static void printList(ListNode head) {
while (head != null) {
System.out.print(head.val);
if (head.next != null) System.out.print(" -> ");
head = head.next;
}
System.out.println();
}
}
class MergeSortedLists {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}
๐ Notes
- ๐ง Use recursion for elegant and concise solutions on small or medium inputs.
- โ๏ธ Use iteration for better performance on large lists (space-efficient).
- โ Always handle null edge cases at the start of your implementation.
Related Posts
Java Interview Guide - Part 4: Advanced Java Concepts (Q31โQ40)
A comprehensive list of essential Java interview questions covering OOP, memory management, exceptions, collections, and Streams
Indepthdev-Backend-Team
Java Interview Guide - Part 1: Core Questions (1โ10)
A comprehensive list of essential Java interview questions covering OOP, memory management, exceptions, collections
Indepthdev-Backend-Team
Java Interview Guide - Part 3: Advanced Java Concepts (Q21โQ30)
A comprehensive list of essential Java interview questions covering OOP, memory management, exceptions, collections, and Streams
Indepthdev-Backend-Team