我已经修改了我的函数以处理偶数节点的情况,但因此我的代码不适用于奇数节点。怎么了?
public LinkedList findMiddleNode() {
Node t1 = this.getHeadNode();
Node t2 = this.getHeadNode();
LinkedList l = new LinkedList();
boolean even = false;
while(t1.getNext() != null) {
t1 = t1.getNext();
if(t1.getNext()!= null && t1.getNext().getNext() != null) {
t1 = t1.getNext();
t2 = t2.getNext();
}
}
if(t1.getNext()!=null)
l.insertFirst(t2.getElement());
else {
l.insertFirst(t2.getElement());
l.insertLast(t2.getNext().getElement());
}
return l;
}