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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started