How can this code be edited to improve the time efficienc?
public static LLList intersect(LLList list1, LLList list2) {
LLList inters = new LLList();
for (int i = 0; i < list1.length(); i++) {
Object item1 = list1.getItem(i);
for (int j = 0; j < list2.length(); j++) {
Object item2 = list2.getItem(j);
if (item2.equals(item1)) {
inters.addItem(item2, inters.length());
break; // move onto the next item from list1
}
}
}
return inters;
}
retainAllmethod of theLinkedListclass or of theArrayListclass to see how it's done there.