I NEED THIS ANSWER FOR MY DATA STRUTURE CLASS:
public static boolean isTrue(int n){
if(n%2 == 0)
return true;
else
return false;
}
public static int Method(int[] numbers, int startIndex) {
if(startIndex >= numbers.length)
return 0;
if (isTrue(numbers[startIndex]))
return 1 + Method(numbers, startIndex + 1);
else
return Method(numbers, startIndex + 1);
}
What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 2, 3, 3, 4, 4, 4, 5, 5}, startIndex = 0
Answer:
Input: 1->2->3->4->5->6->7->8->null
void method(list){
if(list.head == null) return;
Node slow_ref = list.head;
Node fast_ref = list.head;
Node prevS = null;
Node prevF = null;
while(fast_ref != null && fast_ref.next != null){
prevS = slow_ref;
slow_ref = slow_ref.next;
prevF = fast_ref;
fast_ref = fast_ref.next.next;
}
prevS.next = slow_ref.next;
prevF.next.next = slow_ref;
slow_ref.next = null;
}
Answer:
(1):
The Method() code returns the count of even numbers in the given array.
In the question, input array is {1, 2, 2, 3, 3, 4, 4, 4, 5, 5}. In this we have 5 even numbers.
Answer: 5.
(2):
After executing the loop in method(), prevS points to "4", slow_ref points to "5", prevF points to "7".
(a):
prevS.next = slow_ref.next;
After executing this statement, list looks as 1 -> 2 -> 3 -> 4 -> 6 -> 7 -> 8 -> null
(b):
prevF.next.next = slow_ref;
slow_ref.next = null;
After executing these statements, list looks as 1 -> 2 -> 3 -> 4 -> 6 -> 7 -> 8 -> 5 -> null
Answer: 1 -> 2 -> 3 -> 4 -> 6 -> 7 -> 8 -> 5 -> null
Mention in comments if any mistakes or errors are found. Thank you.
Get Answers For Free
Most questions answered within 1 hours.