Java-Node 清晰度

标签 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;
        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; 

}

最佳答案

关于答案

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

是的,两者都可以,headadd 是对同一对象的引用。

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

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

关于Java-Node 清晰度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42382303/

相关文章:

java - 在独立应用程序中使用 Spring

java - 无法在 Spring 中 Autowiring

java - 用JavaParser解析Java代码,找到父节点的语句类型

java - 用头节点反转链表

C++ - 如何创建具有更改名称的类的实例

java - 使用selenium webdriver运行exe文件

java - 使用内部类进行子类化,仅序列化外部类

具有 3 个结构值的 C++ 二叉树

c++ - 从链表中删除节点

java - JUnit、Spring (SpringJUnit4ClassRunner) 和 Spring 接线工作不正常(尝试从 Eclipse 和命令行 Maven 运行)?