In: Computer Science
DATA STRUCTURES
What is returned when calling this method with the singly list contains (1,5,5,7,8)
public static int m1(SinglyList list1)
{
if(list1.size()!=2)
return list1.removeFirst()+m1(list1);
return s;
}
In the above code snippet, 's' is not defined.
Here also removeFirst () is not defined
Returning or addition of an array removeFirst(as the name suggests) and m1(list) is possibly not possible.
if they return length after removal of first and integer in case of s. then the following will hold true.
def abc(list1):
s=0#assumed
if(len(list1)!=2):
return (len(list1)-1)+abc(list1[1:])#[1:] represents the list without the first element
return s
a=[1,5,5,7,8]
print(abc(a))
The expected answer returned is 9
A screenshot is provided for viewing
There is a recursive call function here.
i would like to show you at everystep how the return function is getting called.
for further clarification comment below.