我的方法是首先在列表中找到某个元素,如果为true,则该节点包含的值将移动到列表的前面,而不创建或删除新的节点。我到目前为止,我不认为移动节点部分正在工作,任何帮助表示感谢!将项目移动到LinkedList的前面
public boolean findMove(E e){
Node previous=null;
Node current=head;
while(current !=null){
if(e.equals(current.item)){
previous.next=current.next;
current.next=head;
head=current;
System.out.println("True");
return true;
}
current=current.next;
}
System.out.println("False");
return false;
}
你不是在循环更新'previous'。 –
@John请尝试我的答案,如果它适合你? –
绝对需要'boolean'' return还是可以抛出'NoSuchElementException'?这方面阻碍了你做这个非常干净简洁的方式的能力。 – ChiefTwoPencils