LinkedList<String> list1 = new LinkedList<>();
LinkedList<String> list2 = new LinkedList<>();
String element1 = "1";
String element2 = "2";
String element3 = "3";
String element4 = "4";
String element5 = "5";
list1.addAll(Arrays.asList(element1, element2, element3, element4, element5));
list2.addAll(Arrays.asList(element1, element3, element4, element5));
LinkedList<String> longList = list1.size() >= list2.size() ? list1 : list2;
LinkedList<String> shortList = list1.size() < list2.size() ? list1 : list2;
int diff = longList.size() - shortList.size();
for (int i = 0; i < longList.size(); i++) {
String node1 = list1.poll();
if (i < diff) {
continue;
}
String node2 = list2.poll();
if (node1 == node2) {
System.out.println(node2);
break;
}
}
评论 (1 条评论)