Merge Two Sorted Lists

Merge two sorted linked lists and return the merged sorted list using recursion or iteration.

Merge Two Sorted Lists

๐Ÿงฉ 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 and m 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