Java节点清晰度

时间:2017-02-22 03:43:09

标签: java nodes

我正在尝试将一个Node添加到列表的末尾,这就是我想出的。我只是想知道如果设置tail = head,那是否与tail = add相同?或者,如果我有tail = head.next,如果它与tail = add相同? 提前致谢

public BasicLinkedList<T> addToEnd(T data) {
    Node add= new Node(data);
    Node curr=head;
    if(size==0){
        head= add;
        tail=head;  //is it okay to make this= head? Or should it be =add?
    }else if(size==1){
        head.next=add;
        tail=head.next;  //is it okay to make this= head.next? Or should it be =add?
    }else{
        while(head.next!= null){
            curr=head.next;
        }curr.next = add;
        tail = add;
    }
    size++;
    return this; 

}

1 个答案:

答案 0 :(得分:2)

获得答案

//is it okay to make this= head? Or should it be =add?

是的,没错,head add是对同一对象的引用。

//is it okay to make this= head.next? Or should it be =add?

同样,这里也可以,因为head.nextadd是对同一对象的引用。