Java节点清晰度

Java节点清晰度,java,nodes,Java,Nodes,我试图在列表的末尾添加一个节点,这就是我想到的。我只是想知道我是否设置了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; t

我试图在列表的末尾添加一个节点,这就是我想到的。我只是想知道我是否设置了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; 

}
public BasicLinkedList addToEnd(T数据){
节点添加=新节点(数据);
节点电流=头部;
如果(大小==0){
头=加;
tail=head;//可以将此设置为=head吗?还是应该设置为=add?
}否则如果(大小==1){
head.next=添加;
tail=head.next;//可以设置为this=head.next吗?还是应该设置为=add?
}否则{
while(head.next!=null){
curr=head.next;
}curr.next=添加;
尾=加;
}
大小++;
归还这个;
}
有关

//is it okay to make this= head? Or should it be =add?
是,两者都可以,
head
an
add
是对同一对象的引用

同样,在这里也可以,因为
head。next
add
是对同一对象的引用。

的答案是

//is it okay to make this= head? Or should it be =add?
是,两者都可以,
head
an
add
是对同一对象的引用


同样,在这里也可以,因为
head。next
add
是对同一对象的引用。

我相信您可能有逻辑问题
while(head.next!=null){
,应该检查
curr.next
,否则你会发现自己处于一个无限循环中,因为head在循环中没有变化,所以它总是为true。提示:你不需要
if(size==1)
块。我相信你可能有逻辑问题。
while(head.next!=null){
,应该检查
curr。下一步
否则你会发现自己处于无限循环中,因为头部在循环中没有变化,所以它总是为真。提示:你不需要
if(size==1)
块。