Skip to content

Instantly share code, notes, and snippets.

@czxttkl
Created August 31, 2013 03:22
Show Gist options
  • Save czxttkl/6396038 to your computer and use it in GitHub Desktop.
Save czxttkl/6396038 to your computer and use it in GitHub Desktop.
public class HomeWork1b {
private static <T> void findFirstElement(LinkedList<T> linkA,
LinkedList<T> linkB) {
int offsetA = 0;
int offsetB = 0;
if (linkB.size() > linkA.size())
offsetB = linkB.size() - linkA.size();
else
offsetA = linkA.size() - linkB.size();
int i = offsetA;
while (offsetA < linkA.size()) {
if (!linkA.get(offsetA).equals(linkB.get(offsetB)))
i = offsetA + 1;
offsetA++;
offsetB++;
}
System.out.println("The first overlapped element is " + (i + 1)
+ "th of LinkedListA");
System.out.println("The first overlapped element is "
+ (i + 1 + offsetB - offsetA) + "th of LinkedListB");
}
public static void main(String... args) {
LinkedList<Integer> linkA = new LinkedList<Integer>();
LinkedList<Integer> linkB = new LinkedList<Integer>();
linkA.add(1);
linkA.add(1);
linkA.add(1);
linkA.add(2);
linkA.add(3);
linkA.add(4);
linkA.add(5);
linkB.add(5);
linkB.add(2);
linkB.add(3);
linkB.add(4);
linkB.add(5);
findFirstElement(linkA, linkB);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment