Unsure how to convert this setup into a linkedList setup while still using key in java
private int size = n;
public boolean find(int key) {
int low = 0, high = size-1;
while (low <= high) {
int mid = (low + high) /2;
if (set[mid] == key) return true;
if (key > set[mid] low = mid + 1;
else high = mid - 1;
}
return false;
}
CODE
import java.util.LinkedList;
import java.util.List;
public class Main {
private List<Integer> set = new LinkedList<Integer>();
public boolean find(int key) {
int low = 0, high = set.size()-1;
while (low <= high) {
int mid = (low + high) /2;
if (set.get(mid) == key) return true;
if (key > set.get(mid))
low = mid + 1;
else high = mid - 1;
}
return false;
}
}
NOTE: In order to run this method, first you would need to initialize the elements in set
Get Answers For Free
Most questions answered within 1 hours.