Skip to content

Instantly share code, notes, and snippets.

@xcv58
Created April 25, 2017 03:01
Show Gist options
  • Save xcv58/7667b56cdfe0178b029147464cc64390 to your computer and use it in GitHub Desktop.
Save xcv58/7667b56cdfe0178b029147464cc64390 to your computer and use it in GitHub Desktop.
class MyIter implements Iterator {
private Iter a;
private Iter b;
private Integer common;
public MyIter(Iter a, Iter b) {
if (a == null || b == null) {
throw new Exceptin("Null");
}
this.a = a;
this.b = b;
this.common = null;
}
public Integer next() {
if (!hasNext()) {
throw new Exceptin();
}
Integer res = this.common;
this.common = null;
return res;
}
public boolean hasNext() {
if (this.common != null) {
return true;
}
Integer aa = this.getNext(this.a);
Integer bb = this.getNext(this.b);
if (aa == null || bb == null) {
return false;
}
while (!aa.equals(bb)) {
if (aa < bb) {
aa = this.getNext(this.a);
} else {
bb = this.getNext(this.b);
}
if (aa == null || bb == null) {
return false;
}
}
this.common = aa;
return true;
}
private Integer getNext(Iterator e) {
if (e.hasNext()) {
return e.next();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment