我的方法是首先在列表中找到某個元素,如果爲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