我的方法是首先在列表中找到某个元素,如果为true,则节点包含的值被移动到列表的前面,而不创建或删除新节点。这是我到目前为止,我不认为移动节点部分正在工作,任何帮助都很感激!
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;
}
答案 0 :(得分:2)
previous
。
public boolean findMove(E e){
Node previous=head;
Node current=head;
while(current !=null){
if(e.equals(current.item)){
//Found the item
previous.next=current.next;
current.next=head;
head=current;
System.out.println("True");
return true;
}
previous = current;
current=current.next;
}
System.out.println("False");
return false;
}
答案 1 :(得分:0)
您的代码几乎没有问题:
通过以上内容尝试:
public boolean findMove(E e){
Node previous=null;
Node current=head;
Node headerNode = head;
while(current !=null){
if(e.equals(current.item) && previous != null){
// Update the previous node to point to the next node
previous.next=current.next;
// Move the current node to point to the starting position
current.next=headerNode;
// Mark the current node as header node
headerNode=current;
System.out.println("True");
return true;
}
// Not found - Update Previous to point the current node
previous = current;
current=current.next;
}
System.out.println("False");
return false;
}