5

I'm solving problems for interview practice and I can't seem to figure out the answer to the time and space complexity of the following problem:

Given two sorted Linked Lists, merge them into a third list in sorted order. Let's assume we are using descending ordering.

One of the answers I came across, which is clearly not the most efficient one, is the following recursive solution:

Node mergeLists(Node head1, Node head2) {
    if (head1 == null) {
        return head2;
    } else if (head2 == null) {
        return head1;
    }

    Node newHead = null;
    if(head1.data < head2.data) {
        newHead = head1;
        newHead.next = mergeLists(head1.next, head2);
    } else {
        newHead = head2;
        newHead.next = mergeLists(head1, head2.next);
    }

    return newHead;
}

Now, when I was analyzing the complexity of this function, I came across an issue. I wasn't sure if it was O(M + N) or O(M) + O(N). I just cannot get an intuitive answer. It seems logical to me that the run-time and space complexities of this function are O(N) + O(M) or O(max(N,M)) since the larger value would drive the asymptotic curve (or the recursive calls and stack frame creations).

To sum up:

In big-Oh notation, what is the difference between O(N+M) and O(N) + O(M)? Is there any? If they differ, I would appreciate if somebody could provide simple examples of both.

nem035
  • 34,790
  • 6
  • 87
  • 99
  • Where are you seeing "O(n)+O(m)"? – Dai Aug 16 '14 at 06:51
  • 1
    Well, we are checking all nodes of each list no matter what M and N are? Clearly I am not 100% sure which of the two is the answer since I am asking a question here :) Technically, as I've said above, I think the answer is O(max(N,M)) since the larger value would be the asymptotic bound. – nem035 Aug 16 '14 at 07:38

1 Answers1

10

O(N) + O(M) means functions that are bounded by cN + dM for some c and d.

O(N + M) means functions that are bounded by e(N + M) for some e.

They are equivalent because:

cN + dM <= (c + d)(N + M) for some c and d.

and

e(N + M) <= eN + eM for some e.

fgb
  • 18,439
  • 2
  • 38
  • 52