Question: The following method finds the common elements in two sorted linked lists of integers, building a new sorted linked list that contains copies of the

The following method finds the common elements in two sorted linked lists of integers, building a new sorted linked list that contains copies of the common elements.

Node intersect(Node L1, Node L2) {

if (L1 == null || L2 == null) return null;

if (L1.data == L2.data) {

Node temp = new Node(L1.data, null);

temp.next = intersect(L1.next, L2.next);

return temp; }

if (L1.data < L2.data) return intersect(L1.next, L2);

return intersect(L1, L2.next); }

Assume the L1 list has n nodes and the L2 list has m nodes, and that neither list has any duplicates.

a) What is the worst case number (not big O) of int-to-int comparisons? Clearly show the derivation of this worst case number, including a description of the worst case scenario, and an example that illustrates this scenario. The example must have at least 7 entries in the result list.

b) What is the best case number (not big O) of int-to-int comparisons? Clearly show the derivation of this best case number, including a description of the best case scenario, and an example that illustrates this scenario.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!