0
我排隊做鏈表,但我需要幫助的一個方法率先採取行動的最後一個元素與Node類
public class QueueLinkedList{
private Node first;
private Node last;
public QueueLinkedList(){
first = null;
last = null;
}
class Node{
public Object data;
public Node next;
}
...
public void lastToFirst(){
Node newNode = new Node();
newNode.data = last.data;
newNode.next = first;
first = newNode;
}
,當我跑我的演示類
q.add("1");
q.add("2");
q.add("3");
q.add("4");
q.lastToFirst();
while(!q.isEmpty()){
System.out.print(q.remove());
}
我的預期值4123
,但它給了我41234.
我怎麼能刪除最後一個值4?
所有你需要做的是'last.next = first'; –