0% found this document useful (0 votes)
33 views5 pages

Test Yourself No Marks: Public Void Removeallnodesequaltovalue (T Value)

The document contains 4 questions that test understanding of linked lists, recursion, time complexity analysis, and Big O notation. The questions require implementing a method to remove all nodes with a given value from a linked list, fixing errors in a recursive array summation method, writing a recursive method to count even digits in a number, and analyzing the time complexity of 3 algorithms.

Uploaded by

Ritz Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Test Yourself No Marks: Public Void Removeallnodesequaltovalue (T Value)

The document contains 4 questions that test understanding of linked lists, recursion, time complexity analysis, and Big O notation. The questions require implementing a method to remove all nodes with a given value from a linked list, fixing errors in a recursive array summation method, writing a recursive method to count even digits in a number, and analyzing the time complexity of 3 algorithms.

Uploaded by

Ritz Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Test yourself no Marks

Q1: Here we have two generic classes, one for nodes in a linked list and one for linked
lists. You need to write the implementation for one method in the linked list class.
public class Node<T> {
public T data;
public Node next;

public Node(T value){


this(value,null);
}
public Node (T value, Node nextNode){
data = value;
next=nextNode;
}
}

Write an instance method in the LList<T> class

public void
removeAllNodesEqualToValue (T value)

that removes all the nodes with the given value from the linked list. For
example, if you have the following linked list:

and you called


removeAllNodesEqualToValue(77). Then the third node will be
removed from the list.

1
Your answer

public void removeAllNodesEqualToValue(T value){


Node temp = head, prev = null;
while (temp != null && temp.data == value) {
head = temp.next;
temp = head;
}
while(temp != null){
while(temp != null && temp.data != value){
prev = temp;
temp = temp.next;
}
if(temp == null){
break;
}
prev.next = temp.next;
temp = prev.next;
}
}

Q2: Identify and fix the error(s) in the following


recursive method that finds and return the summation
of array elements
public int sumArray(int a[], int start, int end){

if (start < end)

return 1 + sumArray(start,end);

2
else

return 0;

Your answer
public int sumArray(int a[], int start, int end){
if (start <= end)
return a[start] + sumArray(a, start+1,end);
else
return 0;
}

Q3: Write a recursive method numberOfEvenDigits(int n) that returns the number of


even digits for an integer number (note that we consider 0 to be even).

Call Returns
numberOfEvenDigits (967) 1
numberOfEvenDigits (24) 2

3
numberOfEvenDigits (681) 2
numberOfEvenDigits (-150) 1

Your answer

public int numberOfEvenDigits(int n){


if(n > 0){
int lastDigit = n%10;
if(lastDigit%2 == 0)
return 1 + numberOfEvenDigits(n/10);
else
return numberOfEvenDigits(n/10);
} else{
return 0;

Q4: Find the number of steps, and then time complexity (Big-O) for the following
algorithms?
1) for (i = n; i> 1; i = i / 2) {
j=0
while(j<n){

4
System.out.println(i);
j++;
}

Your answer
Number of steps = n * log n
Time Complexity = O(n log n)

2) i=1;
while (i<n){
i++;
for(j=1; j < n*n ; j++){
System.out.println(i);
}// end for
}// end while

Your answer

Number of steps = n * log n


Time Complexity = O(n log n)

3) 100nlog(n) + 4n2 + logn + 1000

Your answer
Time Complexity = O(n2)

You might also like